Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery-为所有id的背景着色_Jquery_Css_Background Color - Fatal编程技术网

Jquery-为所有id的背景着色

Jquery-为所有id的背景着色,jquery,css,background-color,Jquery,Css,Background Color,在下面的html文件中,jquery没有为所有id=“0”的背景着色。 jquery只对第一个id着色。 如何使所有id=“0”都用jquery着色 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <table> <tr> <td align="center" style="padding-

在下面的html文件中,jquery没有为所有
id=“0”
的背景着色。 jquery只对第一个id着色。 如何使所有
id=“0”
都用jquery着色

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<table>
    <tr>
        <td align="center" style="padding-top: 15px;" id="0">0</td>       
    </tr>
    <tr>
        <td align="center" style="padding-top: 15px;" id="0">0</td>
    </tr>
</table>
<script>
    $(document).ready(function(){
        $("td#0").css("background-color", "rgba(201, 221, 237, 0.99)");
    });
</script>

0
0
$(文档).ready(函数(){
css(背景色),"rgba(201221237,0.99);;
});

id必须是唯一的才能正常工作。当您尝试查找具有重复id的元素时,浏览器将只提供其中一个元素

您应该使用不同的方法查找元素,例如向元素添加一个类,或者将id放在表上并使用它来选择其中的单元格

为了完整性,实际上仍然可以找到具有重复id的所有元素,但是您必须将其视为一个属性:

$("td[id='0']").css("background-color", "rgba(201, 221, 237, 0.99)");

当然,只有当您无法更改HTML代码,并且id是选择元素的唯一方法时,才应该使用它。

id在页面中应该是唯一的。改用类