在一个jQuery选择器中获取一系列元素 [英] Getting a range of elements in one jQuery selector

查看:94
本文介绍了在一个jQuery选择器中获取一系列元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一个表,但是我只想要从第一个索引到最后一个索引的行的子集.我以为你可以这样:

My problem is that I have a table, but I only want a subset of the rows, from first index to last index. I thought you could do it like this:

$('table tr:gt(2):lt(5)');

我认为它只会给您#3和​​#4行,但最终却给您更多的收益.如何告诉选择器仅选择第3行和第4行?

I thought it would give you only rows #3 and #4 but it ends up giving you more than that. How can I tell the selector to choose only rows #3 and #4?

推荐答案

您非常接近,但是选择器中的问题是:lt(5)过滤器.

You're pretty close, but the problem in your selector is the :lt(5) filter.

您想要类似的东西:

$('table tr:gt(2):lt(2)');

区别在于,当应用lt()过滤器时,前三个元素已从集合中删除(通过gt()过滤器).因此,它将获取第3和第4个元素(零索引),而不是第3至第8个元素.

The difference is that by the time the lt() filter is applied, the first three elements were already removed from the set (by the gt() filter). So this will grab the 3rd and 4th element (zero-indexed), instead of the 3rd through the 8th.

这篇关于在一个jQuery选择器中获取一系列元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆