父母与父母祖父母div级检查

时间:2014-10-02 07:51:30

标签: javascript jquery parent window.location

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

e.g。     jQuery(document).ready(function($){

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

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

    // else if matching a-href's grand-parent contains class .bbb
    // then CCC action

})}); 

1 个答案:

答案 0 :(得分:0)

当然,有parenthasClass。另请注意,您不需要(或想要)最初的jQuery("*"),并且您的代码最后会有额外的})

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

    // if matching a-href's parent contains class .aaa
    // then BBB action
    if (parent.hasClass("aaa")) {
        // ...
    }

    // else if matching a-href's grand-parent contains class .bbb
    // then CCC action
    else if (parent.parent().hasClass("bbb")) {
        // ...
    }
}); 
相关问题