2012-08-25 43 views
3

是否有可能在第四个之后选择表格的所有行?我想隐藏其余的。jQuery - 在第四个之后选择所有表格行

<table class="mytable"> 
<tr><td></td></tr> 
<tr><td></td></tr> 
<tr><td></td></tr> 
<tr><td></td></tr> 

<tr><td></td></tr> 
<tr><td></td></tr> 
</table> 

回答

6

您可以使用:gt()选择:

$(".mytable tr:gt(3)").hide();​ 

另一种选择是使用slice()方法:

$(".mytable tr").slice(4).hide();​ 

DEMO:http://jsfiddle.net/F937n/

+0

不是['.slice()'](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice)只是一个本地Javascript方法(和jQuery对象使用它作为本地)? –

+0

@JaredFarrish关注[来源](http://code.jquery.com/jquery.js)jQuery使用本机版本。 – VisioN

+0

哦,你的意思是[这里](https://github.com/jquery/jquery/blob/master/src/core.js#L21)? ';)' –

1

如果一个lways想第四届一个隐藏在完成所有行,那么就不会标准的CSS nth-child更清洁,假设你的browser supported it

.mytable tr:nth-child(n+5) { 
    display:none; 
}​​​​​ 

当然了jQuery相当于将使其工作在较旧的浏览器也:

$('.mytable tr:nth-child(n+5)​​​​​​​​​​​​​​​​​​​​​​​​​​​').hide();​​​​​ 
相关问题