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

时间:2021-12-02 15:41:19

Say I have the following:

说我有以下几点:

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

How would I select all the child elements after the first one using jQuery? So I can achieve something like:

如何使用jQuery在第一个元素之后选择所有子元素?所以我可以做到:

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

8 个解决方案

#1


126  

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

您应该能够使用“not”和“first child”选择器。

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

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

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

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

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

#2


31  

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.

基于我对这四种方法的完全不科学的分析,看起来它们之间的速度差异并不大。我在包含一系列不同长度的无序列表的页面上运行每个列表,并使用Firebug分析器对它们进行计时。

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

Average Time: 5.322ms

平均时间:5.322 ms

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

Average Time: 5.590ms

平均时间:5.590 ms

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

Average Time: 6.084ms

平均时间:6.084 ms

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

Average Time: 7.831ms

平均时间:7.831 ms

#3


13  

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

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

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

#4


9  

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

一个更优雅的查询(选择前面有li元素的所有li元素):

$('ul li+li')

$('李ul李+ ')

#5


2  

This should work

这应该工作

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

#6


2  

i'd use

我使用

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

#7


1  

This is what I have working so far

这就是我到目前为止所做的工作

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

#8


0  

Use jQuery .not() method

使用jQuery自身之外()方法

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

OR

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

#1


126  

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

您应该能够使用“not”和“first child”选择器。

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

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

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

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

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

#2


31  

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.

基于我对这四种方法的完全不科学的分析,看起来它们之间的速度差异并不大。我在包含一系列不同长度的无序列表的页面上运行每个列表,并使用Firebug分析器对它们进行计时。

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

Average Time: 5.322ms

平均时间:5.322 ms

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

Average Time: 5.590ms

平均时间:5.590 ms

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

Average Time: 6.084ms

平均时间:6.084 ms

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

Average Time: 7.831ms

平均时间:7.831 ms

#3


13  

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

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

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

#4


9  

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

一个更优雅的查询(选择前面有li元素的所有li元素):

$('ul li+li')

$('李ul李+ ')

#5


2  

This should work

这应该工作

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

#6


2  

i'd use

我使用

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

#7


1  

This is what I have working so far

这就是我到目前为止所做的工作

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

#8


0  

Use jQuery .not() method

使用jQuery自身之外()方法

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

OR

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