当前网址&链接匹配 - 与div类检查

时间:2014-10-01 14:14:01

标签: javascript jquery window.location

有没有办法测试您当前的网址是否与网页上的链接匹配,还检查匹配的链接是否有特定的div类?

e.g。

jQuery(document).ready(function($){    

// find a-href on page with matching current location URL
jQuery("*").find("a[href='"+window.location.href+"']").each(function(){

    // if matching a-href contains class .aaa
    // then BBB action

    // else if matching a-href contains class .bbb
    // then CCC action

})}); 

1 个答案:

答案 0 :(得分:2)

不确定

jQuery(document).ready(function(){    

    // find a-href on page with matching current location URL
    jQuery("a[href='"+window.location.href+"']").each(function(){

        if ($(this).hasClass('aaa')) {
            // BBB
        }

        if ($(this).hasClass('bbb')) {
            // CCC
        }
    });

});

修改代码:删除与jQuery("*")结合的不必要的find,并删除$作为参数。 (根据意见建议)

相关问题