2017-12-22 17 views
1

일부 항목을 동적으로 수정해야하므로 일부 탐색 HTML을 반복하려고합니다. 구체적으로 Droppable 클래스로 각 항목을 반복하고 특정 자식에 대해 jQuery 객체를 얻는 방법을 설명합니다. 아래에 jquery 객체로 조작해야하는 것들을 나타내는 별표 (*)가있는 아래 코드를 게시했습니다. jQuery로 요소 반복하기

<nav id="sitenav"> 
    <ul class="container ul-reset"> 
     <li class="droppable "> ****** foreach of these 
      <a class="RootNode" id="Help" href="javascript:;">HELP</a> ****** I need this 
      <div class="mega-menu"> 
       <div class="container cf"> 
        <div style="display: inline-block;"> 
         <ul class="ul-reset"> ****** and I need this 
          <a class="heading disabled" id="Help_Help" href="javascript:;"> 
           <h3>Help</h3> 
          </a> 
          <a id="ContactUs" href="/ContactUs/">Contact Us</a> 
          <a id="UserGuides" href="/Help/">User Guides</a> 
         </ul> 
        </div>         
       </div> 
      </div> 
     </li> 

     {more lis with the same structure...} 

    </ul> 
</nav> 

나는 다음 시도했지만 나는 이것이 내가이 현재 jQuery를가 각 루프에서 DOM 요소를 감싸 될 것이라고 생각하기 때문에 내가 생각했던 찾기 메소드를 가지고 있지 않는 오류가 발생합니다.

$("li.droppable").each(function (index) { 
    var header = this.find("a.RootNode"); 
    var col = this.find("ul.ul-reset"); 
}); 

답변

4

.find()는 jQuery를 방법이고 당신은 DOM 객체 this에 호출 할 수 없습니다.

는 대신 jQuery를 객체 $(this).find() 메서드를 호출 할 수있다, 그래서해야한다 :

$("li.droppable").each(function(index) { 
    var $this = $(this); 

    var header = $this.find("a.RootNode"); 
    var col = $this.find("ul.ul-reset"); 
}); 
+2

베스트 DRY 및 캐시'var에 $를 유지하기 위해이 $ (이) =; ' – Jamiec