链接 jQuery 选择器 :lt 和 :gt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1137182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chaining jQuery selectors :lt and :gt
提问by glmxndr
I have a table with more than 9 rows.
我有一个超过 9 行的表。
If I do this : $('table tr:gt(3):lt(6)')
, shall I receive 3 or 6 elements at the end, and why? Are all selectors applied to the same primary selection, or are they successively applied on different selections?
如果我这样做 : $('table tr:gt(3):lt(6)')
,最后我会收到 3 或 6 个元素,为什么?是所有选择器都应用于同一个主要选择,还是相继应用于不同的选择?
回答by Blixt
They're applied sequentially, so first you will filter away the first four elements (:gt(3)
), then you will filter away all elements after the sixth (:lt(6)
) element of the already filtered set.
它们按顺序应用,因此首先您将过滤掉前四个元素 ( :gt(3)
),然后您将过滤掉:lt(6)
已过滤集合的第六个 ( )元素之后的所有元素。
Imagine this HTML:
想象一下这个 HTML:
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
Then do the following jQuery:
然后执行以下 jQuery:
$('br:gt(3):lt(6)').addClass('sel');
You will now have:
您现在将拥有:
<br/><br/>
<br/><br/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br/><br/>
回答by ScottE
I suggest you use the slice() method instead.
我建议你改用 slice() 方法。
http://docs.jquery.com/Traversing/slice#startend
http://docs.jquery.com/Traversing/slice#startend
$('table tr').slice(2, 5).addClass("something");
回答by Russ Cam
Not quite what you might think-
不是你想的那样——
Basically, the second filter is applied sequentially, to the matched set of the first filter.
基本上,第二个过滤器按顺序应用于第一个过滤器的匹配集。
For example, on a table with 10 rows, :gt(3)
will filter to elements 5 - 10, then :lt(6)
will be applied to the 6 elements, not filtering any.
例如,在具有 10 行的表上,:gt(3)
将过滤到元素 5 - 10,然后:lt(6)
将应用于 6 个元素,不过滤任何元素。
if you add /editto the demo URL, you can play with the selector and see for yourself. If you change the second filter to :lt(2)
, you get rows 5 and 6 highlighted in red
如果您将/edit添加到演示 URL,您可以使用选择器并亲自查看。如果将第二个过滤器更改为:lt(2)
,则会以红色突出显示第 5 行和第 6 行
回答by peirix
For some reason :lt(6)
will be ignored in that selection, so it will return everything greater than 3 in this intsance.
由于某种原因:lt(6)
将在该选择中被忽略,因此它将返回此实例中大于 3 的所有内容。
However, if you switch it over, it will work as expected
但是,如果您切换它,它将按预期工作
$('table tr:lt(6):gt(3)')
will return 2 rows (only row 4 and 5 is between 6 and 3).
将返回 2 行(只有第 4 行和第 5 行在 6 和 3 之间)。
**edit:**using v.1.3.2
**编辑:**使用 v.1.3.2
And also, lt(6)
isn't ignored, not just working as I expected it to. So :gt(3):lt(6)
will in fact return 6 elements (if you have enough rows, that is)
而且,lt(6)
并没有被忽视,不仅仅是像我预期的那样工作。所以:gt(3):lt(6)
实际上会返回 6 个元素(如果你有足够的行,那就是)