链接 jQuery 选择器 :lt 和 :gt

发布于 2024-07-28 01:42:02 字数 138 浏览 5 评论 0原文

我有一个超过 9 行的表。

如果我这样做:$('table tr:gt(3):lt(6)'),我最后会收到 3 个或 6 个元素吗?为什么? 所有选择器是否应用于相同的主要选择,或者它们是否连续应用于不同的选择?

I have a table with more than 9 rows.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虫児飞 2024-08-04 01:43:08

由于某种原因 :lt(6) 将在该选择中被忽略,因此它将返回此实例中大于 3 的所有内容。

但是,如果您切换它,它将按预期工作,

$('table tr:lt(6):gt(3)')

将返回 2 行(只有第 4 行和第 5 行位于 6 和 3 之间)。

**编辑:**使用 v.1.3.2

而且,lt(6) 不会被忽略,而不仅仅是按照我的预期工作。 因此 :gt(3):lt(6) 实际上会返回 6 个元素(如果有足够的行的话)

For some reason :lt(6) will be ignored in that selection, so it will return everything greater than 3 in this intsance.

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).

**edit:**using 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)

初熏 2024-08-04 01:42:56

与您想象的不太一样 -

工作演示

基本上,第二个过滤器是按顺序应用的,到第一个过滤器的匹配集。

例如,在有 10 行的表格上,:gt(3) 将过滤到元素 5 - 10,然后 :lt(6) 将应用于 6 个元素,不过滤任何。

如果您将 /edit 添加到演示 URL,您可以使用选择器亲自查看。 如果将第二个过滤器更改为 :lt(2),则第 5 行和第 6 行会以红色突出显示

Not quite what you might think-

Working Demo

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.

if you add /edit to 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

初熏 2024-08-04 01:42:47

我建议您改用 slice() 方法。

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

$('table tr').slice(2, 5).addClass("something");

I suggest you use the slice() method instead.

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

$('table tr').slice(2, 5).addClass("something");
通知家属抬走 2024-08-04 01:42:36

它们按顺序应用,因此首先您将过滤掉前四个元素 (:gt(3)),然后您将过滤掉第六个之后的所有元素 (:lt(6)) 已过滤集合的元素。

想象一下这个 HTML:

<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>

然后执行以下 jQuery:

$('br:gt(3):lt(6)').addClass('sel');

您现在将拥有:

<br/><br/>
<br/><br/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br/><br/>

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.

Imagine this HTML:

<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>

Then do the following 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/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文