我在页面上使用此Javascript来更改单元格的字体颜色和背景颜色,具体取决于内容。当所有上面的单元格都包含一个值时,它工作正常。但是,如果此列中的任何单元格都为空白,则整列不会应用格式。根据内容更改表格单元格颜色 - 上面的空单元格正在打破此代码
任何想法?谢谢。
var allTableCells = document.getElementsByClassName("colhomeaway");
for(var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
//get the text from the first child node - which should be a text node
var currentText = node.childNodes[0].nodeValue;
//check for contents and assign this table cell's background color accordingly
if (currentText === "H") {
node.style.backgroundColor = "#000099";
node.style.color = "white";
node.style.fontWeight = "bold";}
else
if (currentText === "A"){
node.style.backgroundColor = "#99ffff";
node.style.color = "black";
node.style.fontWeight = "bold";
}
}
谢谢,但CSS的方法,似乎没有工作,我真的不希望有,但'&nbsp'在每个空单元格中。 – finisterre