【问题标题】:How do I change the background color of an HTML table cell with javascript?如何使用 javascript 更改 HTML 表格单元格的背景颜色?
【发布时间】:2020-07-08 22:02:33
【问题描述】:

我正在尝试将“takenappt”数组中的表格单元格的背景颜色更改为黑色,而不是默认的白色。这是我目前所拥有的:

<!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 键存储每个表格单元格,然后将某些键放入我的 takeappt 数组中,以便从数组中检索它们并使用它来更改该表格单元格的颜色。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript html html-table colors tablecell


    【解决方案1】:

    = black 行更改为= "black",因为它是一个字符串,并且字符串总是用引号编写。

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2020-04-28
      • 2011-11-06
      相关资源
      最近更新 更多