带有多个表的行条带化

时间:2012-06-07 08:01:26

标签: jquery html-table

我在同一个文档中有多个表,并希望从每个表的第一个tbody行开始添加一个行颜色。

我有跟随代码

$('table tbody tr').filter(':even').not('.spacer, .hidden, thead tr, tfoot tr').addClass('even');

然而,它并不像我想的那样完全正常。任何人都可以帮助上面的代码,所以我可以确保行突出显示从每个表的第一个tbody行开始,无论页面上显示多少个表?

3 个答案:

答案 0 :(得分:1)

您是否在此处查看过:Zebra Striping(来自jQuery文档)。

无论如何,请尝试使用此代码,它可以正常运行:

<script src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("table tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
        $("table tr:even").addClass("alt");
    });
</script>

答案 1 :(得分:1)

根据我的理解,这个选择器适合你:

$('table tbody tr:first-child').addClass('even');

修改

如果你想要从第一行开始划分每隔一行,你需要这样做:

// ... and adding the .even class to :odd rows is just weird.
$('table tbody tr:nth-child(odd)').addClass('even');

答案 2 :(得分:0)

将其更改为$('table tbody:first-child tr:first-child'),这样可确保选中所有表格中第一个tbody的第一行,然后添加其他条件。

http://jsfiddle.net/hnG5w/