jquery selector - exclude first and second

2020-07-03 06:31发布

I want to exclude first and second element from jquery selector. I tried this:

$('p:not(:nth-child(1),:nth-child(2))')

But this only excludes the first element... Thanks

5条回答
狗以群分
2楼-- · 2020-07-03 07:03
 $('p').not(":nth-child(1)").not(":nth-child(2)")

-- SEE DEMO --

查看更多
放荡不羁爱自由
3楼-- · 2020-07-03 07:04

try:

$('p:not(:nth-child(1)),p:not(:nth-child(2))')

Reference: http://www.w3.org/TR/selectors/#negation

from that reference: "The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument...."

查看更多
迷人小祖宗
4楼-- · 2020-07-03 07:07
$('p').not($('p').eq(0), $('p').eq(1)) 
查看更多
欢心
5楼-- · 2020-07-03 07:13

This jQuery sequence uses .slice() to discard the first two elements from the $('p') selector:

$('p').slice(2).

see http://jsfiddle.net/alnitak/zWV7Z/

Note that this is not the same as nth-child - the exclusion is based on the whole set of elements found in the first selector, and not on their relative position in the DOM.

查看更多
我只想做你的唯一
6楼-- · 2020-07-03 07:24

Simply:

$('p:gt(1)')

http://api.jquery.com/gt-selector/

Demo http://jsfiddle.net/NtFYq/1/

As Alnitak has pointed out in the comment, if performance is a concern, you can use his solution of slice

Thanks a lot @Alnitak for pointing that out :)

查看更多
登录 后发表回答