我已经查看了有关如何遍历表的先前答案,但我无法让它工作。我一定是在做一些愚蠢的事情。这是一个例子:
我在 CSS 中有这个
td{width: 30px; height: 30px;
}
<body onload = "setBackground();">
<table id = "table1">
<tr id = "row1">
<td>Stack</td>
<td>Overflow</td>
<td>Is my life saver</td>
</tr>
</table>
</body>
现在,在 JS 我有这个
function setBackground() {
var table = document.getElementById("table1");
//i found this in a previous stack overflow answer and tried it
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
//this is for debugging purposes... I can't even get this to work
alert(table.rows[i].cells[j]);
table.rows[i].cells[j].style.background = "orange"; //just an example
}
}
}
请注意,我在我的 body 标签中调用了该函数,但这是行不通的。我一直在努力解决这个问题,但我做不到!另外,我不知道JQuery。如果有人可以帮助我,我将不胜感激。