jQuery-获取除第一行和最后一行以外的所有行 [英] jquery - get all rows except the first and last
问题描述
我想将一个类动态添加到表的所有行(第一行和最后一行除外).我将如何在不将CSS类分配给行来标识它们的情况下执行此操作.除了第一行外,我得到的全部是
i want to dynamically add a class to all rows of a table except the first and last row. how would i do this without assigning a css class to the rows to identify them. I am getting all but the first row currently with
$("#id").find("tr:gt(0)")
我需要以某种方式将其与not("tr:last")
结合吗?
i need to combine this with not("tr:last")
somehow maybe?
推荐答案
拖放gt()
,因为我认为它比:first
慢一点.
Drop the gt()
, as I'd assume it's a tiny bit slower than :first
.
将not()
与:first
和:last
结合使用:
$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');
如果缺少tbody
元素,这就是为什么直接子选择器失败的原因.没有<tr>
元素作为<table>
标记的直接子代.
Most browsers automatically add an tbody
element in the table markup if that's missing, that is why the immediate children selector was failing – there were no <tr>
elements as an immediate children to the <table>
tag.
我不是100%肯定所有浏览器都采用这种方式,因此手动添加<tbody>
会更安全.否则,您需要稍加嗅探,并且无法将其作为一个衬纸:
I am not 100% sure this is the way all browsers do it, so it would be safer to just add the <tbody>
manually. Otherwise you need a little sniffing and cannot do it as an one liner:
if($('table#tbl > tbody').size() > 0) {
$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');
} else {
$('table#tbl > tr').not(':first').not(':last').addClass('highlight');
}
希望这可以解决您的问题!
Hope this solves your problem!
这篇关于jQuery-获取除第一行和最后一行以外的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!