jquery - 获取除第一行和最后一行之外的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2253658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery - get all rows except the first and last
提问by user210757
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
我想动态地向表的所有行添加一个类,除了第一行和最后一行。如果不为行分配 css 类来识别它们,我将如何做到这一点。我得到了除第一行之外的所有内容
$("#id").find("tr:gt(0)")
i need to combine this with not("tr:last")
somehow maybe?
我需要将它与not("tr:last")
某种方式结合起来吗?
回答by Tatu Ulmanen
Drop the gt()
, as I'd assume it's a tiny bit slower than :first
.
删除gt()
,因为我认为它比:first
.
Use not()
in conjunction with :first
and :last
:
使用not()
结合:first
和:last
:
$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');
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.
tbody
如果缺少,大多数浏览器会自动在表标记中添加一个元素,这就是直接子项选择器失败的原因 - 没有<tr>
元素作为<table>
标记的直接子项。
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:
我不是 100% 确定这是所有浏览器的做法,所以<tbody>
手动添加会更安全。否则,您需要稍微嗅探一下,并且不能将其用作单衬:
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!
希望这能解决您的问题!
回答by mikkelz
Try this:
尝试这个:
.not(':first').not(':last')
回答by ProblemsOfSumit
why not just this?
为什么不只是这个?
$('table tr:not(:first-child):not(:last-child)');
works as pure CSS selector as well.
也可以作为纯 CSS 选择器使用。
回答by GreeKatrina
You can combine the .not()
methods into one by separating the selectors with commas:
您可以.not()
通过用逗号分隔选择器来将这些方法合二为一:
$('#id tr').not(':first, :last');
$('#id tr:not(:first, :last');
Note that the second one is not valid in pure CSS, only as a jQuery selector. For pure CSS you'd have to use @Sumit's answer.
请注意,第二个在纯 CSS 中无效,只能作为 jQuery 选择器。对于纯 CSS,您必须使用 @Sumit 的答案。
回答by Josh Crozier
You can also use the .slice()
methodto remove the first/last elements from the set:
您还可以使用该.slice()
方法从集合中删除第一个/最后一个元素:
$('table tr').slice(1, -1);
The .slice()
methodessentially creates a new jQuery object with a subset of the elements specified in the initial set. In this case, it will exclude the elements with an index of 1
and -1
, which are the first/last elements respectively.
该.slice()
方法本质上是使用初始集合中指定的元素的子集创建一个新的 jQuery 对象。在这种情况下,它将排除索引为1
and的元素-1
,它们分别是第一个/最后一个元素。
Example:
例子:
$('table tr').slice(1, -1).css('background-color', '#f00');
table { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>1</td></tr><tr><td>2</td></tr>
<tr><td>3</td></tr><tr><td>4</td></tr>
<tr><td>5</td></tr><tr><td>6</td></tr>
</table>
Of course, you can also negate :first-child
/:last-child
using :not()
:
当然,你也可以否定:first-child
/:last-child
使用:not()
:
$('table tr:not(:first-child):not(:last-child)').css('background-color', '#f00');
table { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>1</td></tr><tr><td>2</td></tr>
<tr><td>3</td></tr><tr><td>4</td></tr>
<tr><td>5</td></tr><tr><td>6</td></tr>
</table>
You can also combine :nth-child(n+2)
and :nth-last-child(n+2)
:
您还可以组合:nth-child(n+2)
和:nth-last-child(n+2)
:
$('table tr:nth-child(n+2):nth-last-child(n+2)').css('background-color', '#f00');
table { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>1</td></tr><tr><td>2</td></tr>
<tr><td>3</td></tr><tr><td>4</td></tr>
<tr><td>5</td></tr><tr><td>6</td></tr>
</table>
回答by Stuart G
Strange the suggestions posted did not work, they should all work! BUT...
奇怪的建议张贴没有工作,他们应该都工作!但...
If it did not work, do it this way.... slightly slower but MUST WORK!! TRY:
如果它不起作用,请这样做......稍微慢一点但必须工作!尝试:
$('table#tbl tr').addClass('highlight');
$('table#tbl tr:first-child').removeClass('highlight');
$('table#tbl tr:last-child').removeClass('highlight');