2010-08-16 6 views
10

캐 러셀 메뉴 생성을위한 자습서에서 일부 코드를보고 부모가없는 부모 자식 선택자를 발견했습니다. 전에 이것을 본 적이없고 실제로하고있는 일에 혼란스러워했습니다. 다음 코드부모가없는 jquery 자식 선택 자

참조 : 여기

 var $wrapper = $('> div', this).css('overflow', 'hidden'), 
     $slider = $wrapper.find('> ul'), 
     $items = $slider.find('> li'), 
     $single = $items.filter(':first'), 

     singleWidth = $single.outerWidth(), 
     visible = Math.ceil($wrapper.innerWidth()/singleWidth), // note: doesn't include padding or border 
     currentPage = 1, 
     pages = Math.ceil($items.length/visible); 

자습서 : http://jqueryfordesigners.com/jquery-infinite-carousel/

답변

3

부모 (또는이 경우 scope)가 있으며 선택기 안에 플러그인이 적용되는 요소와 관련된 상대 키워드 인 this이 있습니다.

jQuery의 선택기를 사용하면 범위를 설정할 수 있으며 모든 jQuery 요소 객체가 될 수 있습니다.

$(".somediv").myplugin(); 

을 고려 플러그인 내부

$("> div", this) 
is actually translated to 
$("> div", $(".somediv")) 

내 질문 중 하나에서 살펴보고, 대답은 jQuery의 셀렉터에 대한 꽤 설명합니다. What is the fastest method for selecting descendant elements in jQuery?

1
$('> div', this) 

this 중요하다. 그것은

$(this).children('div'); 

동일한 쿼리 자세한 내용은 the documentation for $()를 참조한다 context 매개 변수입니다;

내부적 선택기 컨텍스트 .find() 메소드를 구현 이다 너무 $('span', this) $(this).find('span') 동등하다 : 이는 특히이 언급.

$(this).find('> div')는 "$(this).children('div') 같다 this의 자녀 인 div의 의미

7

컨텍스트와이 선택 :.

$('> div', this) 

이 같은 .find()를 사용하는 주위에 이성을 상실됩니다 :

$(this).find('> div') 

wh > child-selector과 무형 문화 유산 그냥 :

$(this).children('div') 

다른 사람이 같은 거래를하고있다, 그들은 .children()를 사용할 수 있고, 실제로 그렇게하는 것이 더 효율적이 될 것입니다.