2013-07-30 67 views
0

我在页面上使用此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"; 
    } 
} 

回答

1

尝试:

else if(currentTExt === "" or isNaN(currentText) == false){ 

    } 

这笔交易与空单元格时,他们是空白

0

表细胞不正常格式化。使用下面的解决方案之一,你将不必改变你的JavaScript。希望你可以使用这些技术之一。

this link中概述了针对此问题的两种不同解决方案。

CSS修复:

table { empty-cells: show; } 

非打破空间的解决办法:

<td>&nbsp;</td> 
+0

谢谢,但CSS的方法,似乎没有工作,我真的不希望有,但'&nbsp'在每个空单元格中。 – finisterre

相关问题