[英]How to change background color of a table cell in java script using row and column number?
I am trying to change the background color of a cell depending on my row
and col
id.我正在尝试根据我的row
和col
ID 更改单元格的背景颜色。 I have tried like this but didn't work.我试过这样但没有奏效。
let rowNumber = 1; let colNumber = 1; document.getElementById("myTable").rows[rowNumber].cells.item(colNumber).style.backgroundColor = "red";
<table id="myTable"> <tbody> <tr> <td>0-0</td> <td>0-1</td> <td>0-2</td> </tr> <tr> <td>1-0</td> <td>1-1</td> <td>1-2</td> </tr> <tr> <td>2-0</td> <td>2-1</td> <td>2-2</td> </tr> </tbody> </table>
You can do it with CSS like this:你可以用 CSS 来做到这一点:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.