选择除第一个外的所有子元素

假设我有以下几点:

<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>
72362 次浏览

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

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

You should be able to use the "not" and "first child" selectors.

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

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

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

This is what I have working so far

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

This should work

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

A more elegant query (select all li elements that are preceded by a li element):

$('ul li+li')

i'd use

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

Based on my totally unscientific analysis of the four methods here, it looks like there's not a lot of speed difference among them. I ran each on a page containing a series of unordered lists of varying length and timed them using the Firebug profiler.

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

Average Time: 5.322ms

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

Average Time: 5.590ms

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

Average Time: 6.084ms

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

Average Time: 7.831ms

Use jQuery .not() method

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

OR

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