首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery:高亮显示偶数列(而不是行)

问jQuery:高亮显示偶数列(而不是行)
EN

Stack Overflow用户
提问于 2010-07-06 23:03:43
回答 2查看 1.8K关注 0票数 6

如何使用jQuery为偶数(或奇数)表列着色?(无需手动添加类)

我的标记是:

代码语言:javascript
复制
<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标记)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-06 23:20:46

您可以使用:nth-child() selector来执行以下操作:

代码语言:javascript
复制
$('table tr :nth-child(2n)').css('background-color', '#eee');

You can see a demo here,这个版本处理列,不管单元格是<th>还是<td>,如果你只想做其中的一个,你可以在那里添加(例如td:nth-child(2n))。如果要选择其他列,只需执行2n+1即可。​

票数 7
EN

Stack Overflow用户

发布于 2010-07-06 23:24:09

编辑:已更新,修复了我对问题的误读。

这应该是可行的:

代码语言:javascript
复制
$('tr > :nth-child(even)').css('background-color','#eee');

或

代码语言:javascript
复制
$('tr > :nth-child(odd)').css('background-color','#eee');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3187434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档