javascript
  • jquery
  • parent
  • window.location
  • 2014-10-02 141 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")) { 
         // ... 
        } 
    }); 
    
    相關問題