简体   繁体   English

Jquery选择器用于第一个“一,二,三和X”元素和:not()?

[英]Jquery selector for first 'one, two, three, and X' elements and :not()?

If you want to select the first element or :not, there are several choices: 如果要选择第一个元素或者不是,则有以下几种选择:

  • :not(:first)
  • :nth-child(1)
  • first()
  • :first-child
  • etc... 等等...

What are the options for selecting the first 'two, three, and X' elements? 选择第一个“两个,三个和X”元素有哪些选项?

AND

:not(the first two, three, and X?) can the :not() be accomplished by using the JS ! :not(the first two, three, and X?)可以:not()通过使用JS完成!

If the elements you want to exclude follow consecutively, you could use slice() : 如果要排除的元素连续跟随,则可以使用slice()

$(selector).slice(0,3);

JS Fiddle demo . JS小提琴演示

This will select the first three elements, the 0 is the zero-based index of the first element to start, and the second, the 3 , is the zero-based index of the element at which to stop, collecting. 这将选择前三个元素, 0是要开始的第一个元素的从零开始的索引, 第二个元素3是要停止,收集的元素的从零开始的索引。

The second number can also be a negative number and, if so, will count from the end of the returned set of elements: 第二个数字也可以是负数,如果是,则从返回的元素集的末尾开始计算:

$(selector).slice(1,-3);

JS Fiddle demo . JS小提琴演示

References: 参考文献:

There are a few ways: 有几种方法:

For just the first 1 or 2 you could specify using eq : $('.class').eq(0); 对于前1或2,您可以使用eq$('.class').eq(0);指定$('.class').eq(0);

For a limited range, use lt : $('.class').lt(3); 对于有限范围,请使用lt$('.class').lt(3);

To perform the "not" of lt , use gt , which is greater-than. 要执行lt的“not”,请使用gt ,大于。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM