简体   繁体   English

如何在表格中更改tr td的背景颜色

[英]How to change background color of tr td within a table

I am trying to set the background color of td elements in my table, but the browser keeps saying "Cannot read property 'children' of undefined" JS is able to navigate thru all the records, but the color never changes due to the error. 我正在尝试在表中设置td元素的背景颜色,但是浏览器一直在说“无法读取未定义的属性'children'”, JS可以浏览所有记录,但是颜色不会因错误而改变。

This is my table 这是我的桌子

<div id="GruposCxPPago">
 <table>
  <tr>
    <td>text1</td>
    <td>text2</td>
  </tr>
 </table>
</div>

This is my Javascript 这是我的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"});
      });
}); 

Use simply this: $("#GruposCxPPago td").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> 

you can do this 你可以这样做

 $("#GruposCxPPago td").each(function() $(this).css({"background-color":"red"}); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM