Adr*_*ian 5 html css jquery 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">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>
Run Code Online (Sandbox Code Playgroud)
(它可能包含也可能不包含范围属性或th标签)
您可以使用:nth-child()选择器:
$('table tr :nth-child(2n)').css('background-color', '#eee');
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到演示,这个版本确实列,如果不考虑细胞是<th>或<td>可以添加在那里(例如td:nth-child(2n))如果你想要做的只是一个或另一个.如果要选择其他列,请2n+1改为执行.