jquery - 使用 jquery 选择每隔 2 个表行

标签 jquery jquery-selectors

我目前正在使用此代码向表中的每隔一行添加一个类。

$(".stripeMe tr:even").addClass("alt");

但是,在另一个表上,我想向第 3,4、7,8、11,12 行等添加一个类...

这可能吗?

最佳答案

你需要这样做:

$(".stripeMe tr:nth-child(4n)").add(".stripeMe tr:nth-child(4n-1)").addClass("alt");​​​​​​​​
//or...
$("tr:nth-child(4n), tr:nth-child(4n-1)", ".stripeMe").addClass("alt");​​​​​​​​​​​​​​​​​

You can see this working here .

使用这个:

$(".stripeMe tr:nth-child(4n), .stripeMe tr:nth-child(4n-1)").addClass("alt");​​​​​​​​

gets different results (即在 webkit 中,也可能在其他中)。

关于jquery - 使用 jquery 选择每隔 2 个表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2782088/

相关文章:

javascript - 如何使 div 与页面上的特定 td 对齐?

javascript - WebClientPrint 用于 Chrome 上的客户端打印 javascript 问题

jquery-selectors - 使用 jquery 选择器获取 tinymce textarea 的值

javascript - 如果选中所有子复选框,如何选中父复选框

jQuery - :not selector 内不区分大小写的值

javascript - jquery:选择具有类名和节点索引的元素

javascript - jQuery getelementsbyclassname 等效项

javascript - 按钮上的 jQuery 单击事件不会将值分配给变量

jquery - 查找 id 属性开头的 select 元素时出现问题

javascript - 带变量的跨浏览器 CSS3 转换 jQuery 函数