jQuery:突出显示偶数列(而不是行)
如何使用 jQuery 对偶数(或奇数)表列进行着色? (无需手动添加类)
我的标记是:(
<table>
<caption>Table caption</caption>
<thead>
<tr><th scope="col">Table header 1</th><th scope="col">Table header 2</th><th scope="col">Table header 3</th><th scope="col">Table header 3</th></tr>
</thead>
<tbody>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
</tbody>
</table>
它可能包含也可能不包含范围属性或 th
标签)
How to colorize even (or odd) table columns using jQuery?
(without adding classes manually)
My markup is:
<table>
<caption>Table caption</caption>
<thead>
<tr><th scope="col">Table header 1</th><th scope="col">Table header 2</th><th scope="col">Table header 3</th><th scope="col">Table header 3</th></tr>
</thead>
<tbody>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
<tr><th scope="row">Table row header</th><td>Table data</td><td>Table data</td><td>Table data</td></tr>
</tbody>
</table>
(it may or may not contain scope attribs or th
tags)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
:nth-child()
选择器为此:您可以在此处查看演示,此版本执行列,无论单元格是否为
或
如果您愿意,您可以在其中添加它(例如
td:nth-child(2n)
)只有其中之一。如果您想选择其他列,只需执行2n+1
即可。You can use the
:nth-child()
selector for this:You can see a demo here, this version does the columns, regardless if the cell is a
<th>
or<td>
you can add that in there (e.g.td:nth-child(2n)
) if you want to do only one or the other. If you want to select the other columns, just do2n+1
instead.
编辑:更新以纠正我对问题的误读。
这应该有效:
或
Edit: Updated to fix my misreading of the question.
This should work:
or