prosource

첫번째 요소를 제외한 모든 하위 요소 선택

probook 2023. 11. 4. 10:53
반응형

첫번째 요소를 제외한 모든 하위 요소 선택

제게 다음과 같은 것이 있다고 가정해 보겠습니다.

<ul>
 <li>First item</li>
 <li>Second item</li>
 <li>Third item</li>
</ul>

jQuery를 사용하여 첫 번째 요소 이후의 모든 하위 요소를 선택하려면 어떻게 해야 합니까?그래서 다음과 같은 성과를 얻을 수 있습니다.

<ul>
 <li>First item</li>
 <li class="something">Second item</li>
 <li class="something">Third item</li>
</ul>

"아니요" 및 "첫 번째 자식" 선택기를 사용할 수 있어야 합니다.

$("li:not(:first-child)").addClass("something");

http://docs.jquery.com/Selectors/not

http://docs.jquery.com/Selectors/firstChild

여기서 네 가지 방법에 대해 전혀 과학적이지 않은 분석을 해본 결과, 그 중에서도 속도 차이가 별로 나지 않는 것 같습니다.길이가 다른 일련의 순서 없는 목록이 들어 있는 페이지에서 각각 실행하고 Firebug 프로파일러를 사용하여 시간을 측정했습니다.

$("li").slice(1).addClass("something");

평균 시간: 5.322ms

$("li:gt(0)").addClass("something");

평균 시간 : 5.590ms

$("li:not(:first-child)").addClass("something");

평균 시간: 6.084ms

$("ul li+li").addClass("something");

평균시간 : 7.831ms

http://docs.jquery.com/Traversing/slice

$("li").slice(1).addClass("something");

보다 우아한 쿼리(lie element 앞에 오는 모든 lie element를 선택):

$('ul li+li')

이 방법이 효과가 있을 것입니다.

$('ul :not(:first-child)').addClass('something');

쓰겠습니다

$("li:gt(0)").addClass("something");

이것이 제가 지금까지 해온 일입니다.

$('.certificateList input:checkbox:gt(0)')

jQuery.not() 메서드 사용

$('li').not(':first').addClass('something');

오어

var firstElement = $('li').first();
$('li').not(firstElement).addClass('something');

언급URL : https://stackoverflow.com/questions/178407/select-all-child-elements-except-the-first

반응형