Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery:高亮显示偶数列(非行)_Jquery_Html_Css_Jquery Selectors - Fatal编程技术网

jQuery:高亮显示偶数列(非行)

jQuery:高亮显示偶数列(非行),jquery,html,css,jquery-selectors,Jquery,Html,Css,Jquery Selectors,如何使用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">T

如何使用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>

表格标题
表头1表头2表头3表头3
表行标题表数据表数据表数据
表行标题表数据表数据表数据
表行标题表数据表数据表数据
表行标题表数据表数据表数据
(它可能包含范围属性或
th
标记)

您可以使用:

$('table tr :nth-child(2n)').css('background-color', '#eee');
,此版本执行列操作,无论单元格是
还是
,如果您只想执行其中一项或另一项操作,则可以将其添加到其中(例如
td:nth child(2n)
)。如果要选择其他列,只需执行
2n+1

编辑:更新以修复我对问题的误读

这应该起作用:

$('tr > :nth-child(even)').css('background-color','#eee');


+1:谢谢你在我的回答中的评论,并在这里提供了正确的解决方案。谢谢,这个很好用(以前的版本做了一些检查;)没有JS这可能吗?仅限CSS3?值得注意的是,如果表中有任何行,我认为这将不起作用,因为第二个子行将不再是第二列。谢谢链接,我也不知道这个工具。Ken-OP是为列着色,而不是为行着色o) 这是对行着色,我需要columnsKen-您忽略了OP所说的可能存在或不存在的
th
元素o)
$('tr > :nth-child(odd)').css('background-color','#eee');