Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果URL有子目录,则使用Javascript_Javascript_Jquery_String_Parsing_Url - Fatal编程技术网

如果URL有子目录,则使用Javascript

如果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

如果URL包含一个子目录,但没有任何结果,尝试想出一种优雅的方法来删除它。我很乐意接受你的建议。我当前的实现仅限于站点根目录下的文件。需要更灵活的东西

另一个细节是它需要与子目录无关,因此没有
.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 */
});