如果URL有子目录,则使用Javascript
如果URL包含一个子目录,但没有任何结果,尝试想出一种优雅的方法来删除它。我很乐意接受你的建议。我当前的实现仅限于站点根目录下的文件。需要更灵活的东西 另一个细节是它需要与子目录无关,因此没有如果URL有子目录,则使用Javascript,javascript,jquery,string,parsing,url,Javascript,Jquery,String,Parsing,Url,如果URL包含一个子目录,但没有任何结果,尝试想出一种优雅的方法来删除它。我很乐意接受你的建议。我当前的实现仅限于站点根目录下的文件。需要更灵活的东西 另一个细节是它需要与子目录无关,因此没有.split('nameOfSubdirectory')业务 把它放在上下文中,我使用AJAX,并使用目标变量打印到页面的url中,所以我喜欢使其美观 使用以下方法,单击a[href=“otherPage.php”]返回otherPage。如果单击a[href=“subPage/differentitpag
.split('nameOfSubdirectory')
业务
把它放在上下文中,我使用AJAX,并使用目标变量打印到页面的url中,所以我喜欢使其美观
使用以下方法,单击a[href=“otherPage.php”]
返回otherPage
。如果单击a[href=“subPage/differentitpage.html”]
它将返回subPage
。不行
$('a').click(function(e){
var thiis = $(this),
href = thiis.attr('href'),
target = href.split('/')[1].split('.')[0];
console.log(target);
/* Does stuff */
});
也许可以尝试分配第一次拆分操作,然后使用该结果的长度作为指导:
$('a').click(function(e){
var thiis = $(this),
href = thiis.attr('href'),
split_results = href.split('/')[1];
target = split_results[split_results.length-1].split('.')[0];
console.log(target);
/* Does stuff */
});
或者,如果有点混乱,也可以通过内联方式完成
$('a').click(function(e){
var thiis = $(this),
href = thiis.attr('href'),
target = href.split('/')[href.split('/').length-1].split('.')[0];
console.log(target);
/* Does stuff */
});