jQuery |如果url的哈希值等于匹配的锚标记,则模拟单击

jQuery |如果url的哈希值等于匹配的锚标记,则模拟单击,jquery,Jquery,如果url与锚定标记的href值匹配,我尝试模拟单击我的一个选项卡 这就是我迄今为止一直在尝试的: if (window.location.hash.indexOf('!/') !== -1) { $('#nav li a').each(function(i) { if ($(this).attr('href').indexOf('#' + window.location.hash.substring(3)) !== -1) $(this).t

如果url与锚定标记的
href
值匹配,我尝试模拟单击我的一个选项卡

这就是我迄今为止一直在尝试的:

if (window.location.hash.indexOf('!/') !== -1) {

    $('#nav li a').each(function(i) {

        if ($(this).attr('href').indexOf('#' + window.location.hash.substring(3)) !== -1)
            $(this).trigger('click');
    });
}
我的
散列
看起来像
/有些东西和我的
url
看起来像
http://domain.com/#!/一些东西


所以,我检查一下是否
/
在URL中,然后我想单击我导航中的链接,该链接有一个匹配的
href
#something

您可以使用jQuery搜索hashbang之后的内容

未测试代码:

if (window.location.hash.indexOf('!/') !== -1) {
    $('#nav li a[href$='+window.location.hash+']').click();
}

编辑:根据另一个答案,只需将changing
href=
添加到
href$=
即可使此跨浏览器兼容

window.location.hash
在某些浏览器中返回包含
#
的哈希值,而在某些浏览器中则不返回,因此在比较之前,应将其替换为零。试试这个

if (window.location.hash.indexOf('!/') !== -1) {

    $('#nav li a').each(function(i) {

        if ($(this).attr('href').indexOf('#' + window.location.hash.replace('#', '').substring(3)) !== -1)
            $(this).trigger('click');
    });
}

我试过了,但它似乎不想找到带有匹配href的锚并触发单击