jQuery-获取除第一行和最后一行以外的所有行 [英] jquery - get all rows except the first and last

查看:499
本文介绍了jQuery-获取除第一行和最后一行以外的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个类动态添加到表的所有行(第一行和最后一行除外).我将如何在不将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屋!

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