如何使用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标记)
发布于 2010-07-06 23:20:46
您可以使用:nth-child() selector来执行以下操作:
$('table tr :nth-child(2n)').css('background-color', '#eee');You can see a demo here,这个版本处理列,不管单元格是<th>还是<td>,如果你只想做其中的一个,你可以在那里添加(例如td:nth-child(2n))。如果要选择其他列,只需执行2n+1即可。
发布于 2010-07-06 23:24:09
编辑:已更新,修复了我对问题的误读。
这应该是可行的:
$('tr > :nth-child(even)').css('background-color','#eee');或
$('tr > :nth-child(odd)').css('background-color','#eee');https://stackoverflow.com/questions/3187434
复制相似问题