<table>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
</table>
table td {
background-color: green;
padding: 5px;
border: 1px solid blue;
}
.red {
background-color: red;
}
LIVE: http://jsfiddle.net/zCduV/1/
如何在此表中为第二列添加带有jQuery的类.red(在此示例中,这是td中ccc的位置)?
答案 0 :(得分:13)
这可能吗?
// selects both table header and table data cells from the second column of the table
$('table th:nth-child(2), table td:nth-child(2)').addClass('red');
答案 1 :(得分:11)
使用jQuery nth-child选择器:
$('td:nth-child(2)').addClass('red');
答案 2 :(得分:2)
看一下jQuery nth-child-selector。这就是你要找的东西。
$('td:nth-child(2)').addClass('red');
答案 3 :(得分:2)
$('td:nth-child(2)').addClass('red');
答案 4 :(得分:1)
Example此解决方案。