javascript
  • jquery
  • parent
  • window.location
  • 2014-10-02 265 views -1 likes 
    -1

    有没有一种方法来测试您的当前URL位置是否与您网页上的链接相匹配,但是还要检查匹配链接的父母(或祖父母)div是否具有特定的div类?父母和祖父母div类检查

    例如 jQuery的(文件)。就绪(函数($){

    // 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 
    
    })}); 
    

    回答

    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")) { 
         // ... 
        } 
    }); 
    
    相关问题