2011-10-12 1 views
12

는 HTML을 감안할 때 :parentsUntil()이 가장 좋은 함수인가요? jQuery를

 <div class="a"> 
     <div class="b"> 
      <div class="c"> 
      <div class="d"> 
      </div> 
      </div> 
      <div class="d"> 
      </div> 
     </div> 
    </div> 

임 클래스 'D'와 모든 요소를 ​​클릭 할 때 적용된 클래스 a로 선택 부모 요소에 관심.

다음 javascript/jQuery가 있지만 매우 혼란스럽게 보입니다. 더 좋은 방법이 있습니까?

당신 want $(this).closest('.a')
<script> 
    $('.d').click(function(){ 
     var elementA = $(this).parentsUntil('.a').last().parent();  
    }) 
</script> 

답변

3

당신도 할 수 있습니다

$(".d").click(function() { 
    // parents() will walk up through parent nodes. If you 
    // pass a selector, the set will be filtered. If not, 
    // you get the full list of parent elements. 
    var elementA = $(this).parents(".a"); 
}); 

문서 here.

희망이 도움이됩니다. 건배.

관련 문제