简体   繁体   English

根据内容更改表格单元格颜色-上方的空白单元格破坏了此代码

[英]Changing table cell colour based on content - Empty cell above is breaking this code

I have this Javascript on my page, to change the font colour and background colour of a cell depending on the contents. 我的页面上有此Javascript,用于根据内容更改单元格的字体颜色和背景颜色。 It works fine when all the cells above contain a value. 当所有上面的单元格都包含一个值时,它工作正常。 However, when any of the cells above in this column are blank, the whole column doesn't get the formatting applied. 但是,当此列上方的任何单元格为空白时,整列都不会应用格式。

Any ideas? 有任何想法吗? Thanks. 谢谢。

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";
   }
}

Try : 尝试:

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

    }

This deal with the empty cells 处理空单元格

Table cells don't format properly when they are blank. 表格单元格为空白时,格式不正确。 Using one of the solutions below, you won't have to change your javascript at all. 使用以下解决方案之一,您完全不需要更改JavaScript。 Hopefully you can use one of these techniques. 希望您可以使用这些技术之一。

Two different solutions to this issue as outlined in this link . 该链接所述,有两种不同的解决方案。

CSS fix: CSS修复:

table { empty-cells: show; }

Non breaking space fix: 非中断空间修复:

<td>&nbsp;</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM