无法设置"TD"的背景颜色.使用JQuery [英] Cannot set the background color of "TD" using JQuery

查看:257
本文介绍了无法设置"TD"的背景颜色.使用JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一张桌子

<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" 的背景色.我尝试过

I want to set the background color of only the "td" elements of the table and not "th". I have tried with

$("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的评论,您也可以执行以下操作:(但不是必需的)

According to @Matt Huggins's comment, you can do like following also:(but not necessary)

$("table tbody td").css('background-color', '#00ff00');

演示

DEMO

这篇关于无法设置"TD"的背景颜色.使用JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆