[英]How to change background color of tr td within a table
我正在嘗試在表中設置td元素的背景顏色,但是瀏覽器一直在說“無法讀取未定義的屬性'children'”, JS可以瀏覽所有記錄,但是顏色不會因錯誤而改變。
這是我的桌子
<div id="GruposCxPPago">
<table>
<tr>
<td>text1</td>
<td>text2</td>
</tr>
</table>
</div>
這是我的Javascript
var ele = new Array();
$("#GruposCxPPago").find('tr').each(function(i)
{
var item = ele[i];
item.children('td').each(function(tdEL) {
tdEl.css({"background-color":"red"});
});
});
只需使用以下代碼: $("#GruposCxPPago td").css({"background-color":"red"});
$("#GruposCxPPago td").css({"background-color":"red"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="GruposCxPPago"> <table> <tr> <td>text1</td> <td>text2</td> </tr> </table> </div>
你可以這樣做
$("#GruposCxPPago td").each(function() $(this).css({"background-color":"red"}); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.