簡體   English   中英

根據內容更改表格單元格顏色-上方的空白單元格破壞了此代碼

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

我的頁面上有此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";
   }
}

嘗試:

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

    }

處理空單元格

表格單元格為空白時,格式不正確。 使用以下解決方案之一,您完全不需要更改JavaScript。 希望您可以使用這些技術之一。

該鏈接所述,有兩種不同的解決方案。

CSS修復:

table { empty-cells: show; }

非中斷空間修復:

<td>&nbsp;</td>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM