简体   繁体   English

具有隐藏行的表的JQuery选择器 - 备用行着色

[英]JQuery Selector for table with hidden row - alternate row colouring

I have a table, and usually i use this selector to apply odd and even row: 我有一个表,通常我使用这个选择器来应用奇数和偶数行:

table.find('tbody tr:even').addClass('even');
table.find('tbody tr:odd').removeClass('even');

My Table has rows being inserted at various places, hence why i remove it from the odd rows. 我的表有各行插入的行,因此我从奇数行中删除它。

I now have certain rows hidden using 我现在隐藏了某些行

jQueryTrObject.hide();

I want to apply the same styling as before, so that alternate rows, as far as the user is concerned are marked up odd and even, and i'd like it to take into account of hidden rows. 我想要应用与以前相同的样式,以便备用行,就用户而言被标记为奇数和偶数,并且我希望它考虑到隐藏的行。

How do i write a selector to do this, for do i have to use the each and specifically check? 我如何编写选择器来执行此操作,因为我必须使用每个选项并专门检查吗?

use the :visible selector 使用:visible选择器

table.find('tbody tr.even').removeClass('even');
table.find('tbody tr:visible:even').addClass('even');

Remember to use it first so that the :even filter applies after it. 请记住先使用它,以便:even过滤器适用于它之后。

Try this out: 试试这个:

table.find('tbody tr').removeClass('even')
    .filter(':visible:even').addClass('even');

使用:not(:hidden)选择器

table.find('tbody tr:not(:hidden):even').addClass('even');

You can use the :visible selector to only markup visible row: 您可以使用:visible选择器仅标记可见行:

table
    .find('tbody tr:visible:even')
    .addClass('even')
.end()
    .find('tbody tr:visible:odd')
    .removeClass('even');
.end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM