如何使用 javascript 更改 HTML table 单元格的背景颜色?
How do I change the background color of an HTML table cell with javascript?
我正在尝试将 "takenappt" 数组中 table 个单元格的背景颜色更改为黑色,而不是默认的白色。这是我目前所拥有的:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<table id="table">
</table>
<script type="text/javascript">
//this code prints out the table.
let table = document.getElementById("table");
for (a = 0; a < 3; a++) {
let row = table.insertRow(a);
for (b = 0; b < 3; b++) {
row.insertCell(b);
table.rows[a].cells[b].innerHTML = a;
table.rows[a].cells[b].id = a + ":" + b;
}
}
var takenappt = ["0:1", "1:2"];
var repeat = takenappt.length;
//this code is supposed to go through my takenappt array and change the background color of certain table cells.
for (i = 0; i < repeat; i++) {
var appt = document.getElementById(takenappt[i]);
appt.style.backgroundColor = black;
}
</script>
</body>
</html>
我试图用 row:column 键存储每个 table 单元格,然后将某些键放入我的 takenappt 数组中,以便从数组中检索它们并使用它来更改table 单元格的颜色。任何帮助将不胜感激。
将行 = black
更改为 = "black"
因为它是一个字符串,而且字符串总是用引号括起来。
我正在尝试将 "takenappt" 数组中 table 个单元格的背景颜色更改为黑色,而不是默认的白色。这是我目前所拥有的:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<table id="table">
</table>
<script type="text/javascript">
//this code prints out the table.
let table = document.getElementById("table");
for (a = 0; a < 3; a++) {
let row = table.insertRow(a);
for (b = 0; b < 3; b++) {
row.insertCell(b);
table.rows[a].cells[b].innerHTML = a;
table.rows[a].cells[b].id = a + ":" + b;
}
}
var takenappt = ["0:1", "1:2"];
var repeat = takenappt.length;
//this code is supposed to go through my takenappt array and change the background color of certain table cells.
for (i = 0; i < repeat; i++) {
var appt = document.getElementById(takenappt[i]);
appt.style.backgroundColor = black;
}
</script>
</body>
</html>
我试图用 row:column 键存储每个 table 单元格,然后将某些键放入我的 takenappt 数组中,以便从数组中检索它们并使用它来更改table 单元格的颜色。任何帮助将不胜感激。
将行 = black
更改为 = "black"
因为它是一个字符串,而且字符串总是用引号括起来。