我有一个如下表格
<table >
<tr>
<th scope="col">EmpId</th><th scope="col">EmpName</th>
</tr>
<tr>
<td>1</td><td>ABC</td>
</tr>
<tr>
<td>2</td><td>DEF</td>
</tr>
</table>
我只想设置表格的“td”元素的背景颜色,而不是“th”。我尝试过
$("table").children("td").css('background-color', '#00ff00');
OR
$("table").children("tr").children("td").css('background-color', '#00ff00');
但是没有结果。
请帮忙看看我犯了什么错误?
谢谢
最佳答案
您可以尝试如下:
$("table tr").find("td").css('background-color', '#00ff00');
或
$("table td").css('background-color', '#00ff00');
<强> DEMO
根据@Matt Huggins的评论,您也可以执行以下操作:(但不是必需的)
$("table tbody td").css('background-color', '#00ff00');
<强> DEMO
关于jquery - 无法使用JQuery设置 "TD"的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875553/