我想隐藏行索引中的行,从1到3.以下代码无效:
$("table").find("tr:gt(1)").find("tr:lt(3)").each(function () { $(this).hide() });
答案 0 :(得分:2)
尝试使用the slice
method。
来自文档:
描述: 将匹配元素集减少到由一系列索引指定的子集
.slice(开始[,结束])
start 一个整数,指示开始选择元素的从0开始的位置。如果为负数,则表示距离集合末尾的偏移量。
end 一个整数,表示从中停止选择元素的从0开始的位置。如果为负,则表示距离集合末尾的偏移量。如果省略,则范围将持续到集合结束。
代码:
$("table tr").slice(1, 3 + 1).hide();
另请注意,the hide
method不要求您使用each
:
隐藏匹配的元素
答案 1 :(得分:2)
您可以使用filter
将匹配的元素集减少到正确范围内的元素:
$("table tr").filter(function(i) {
return i > 1 && i < 3;
}).hide();
请注意,我没有使用each
在最后迭代匹配元素集,因为hide
与大多数jQuery方法一样,无论如何都适用于集合中的所有元素。
答案 2 :(得分:2)
第一个find()返回匹配的元素,然后第二个过滤该选择。
尝试此选择器
$('table tr:gt(0):lt(4)').hide();
此外,slice()将是一个很好的选择(如果不是最好的)
$('table tr').slice(1,4).hide();
答案 3 :(得分:1)
像这样的选择器?
$('table tr:gt(0):lt(4)').hide();
我不知道你是否想要指数1
和3
,所以现在我假设你这样做。
如果你不能,你可以使用:
$('table tr:gt(1):lt(3)').hide();
但那只是索引2
:
$('table tr:eq(2)').hide();
如果您需要将$('table')
作为基本选择器,则可以使用find
方法:
$('table').find('tr:gt(#):lt(#)').hide().end().chain().more().stuff()