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

    현재 URL 위치가 페이지의 링크와 일치하는지 테스트 할 수 있습니까? 또한 일치하는 링크의 부모 (또는 조부모님) div가 특정 div 클래스를 갖고 있는지 확인하는 방법이 있습니까?부모 및 조부모 div 클래스 검사

    // 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

    물론 jQuery를 (문서) .ready (기능 ($)는 {, 또한 초기 jQuery("*") 있음)가 필요합니다 (또는하지 마십시오. parenthasClass이, 그리고 당신의 코드를 추가 })에있다 끝 :

    // 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")) { 
         // ... 
        } 
    }); 
    
    관련 문제