簡體   English   中英

更改表格單元格的內容

[英]Changing the Contents of Table Cells

我是 JavaScript 的新手,我的目標是根據表格的內容更改表格每個單元格中文本的顏色。 出於某種原因,它僅適用於第一列。

 function key() { var test = document.getElementById("schedule"), testChildS, testChild; var rows = test.rows.length; var columns = test.rows[0].cells.length; for (var r = 0; r < rows; r++) { for (var c = 0; c < columns; c++) { testChildS = test.rows[r].cells[c].innerHTML.toLowerCase(); testChild = test.rows[r].cells[c]; if (testChildS.includes("private")) { testChild.style.color = "red"; } else if (testChildS.includes("qi gung")) { testChild.style.color = "tan"; } else if (testChildS.includes("mini")) { testChild.style.color = "lightblue"; } else if (testChildS.includes("kids")) { testChild.style.color = "blue"; } else if (testChildS.includes("all")) { testChild.style.color = "pink"; } else if (testChildS.includes("instructor")) { testChild.style.color = "purple"; } else if (testChildS.includes("yang")) { testChild.style.color = "lightgreen"; } else if (testChildS.includes("chen")) { testChild.style.color = "green"; } else if (testChildS.includes("lion")) { testChild.style.color = "pink"; } else if (testChildS.includes("adult")) { testChild.style.color = "blue"; } else if (testChildS.includes("chinese")) { testChild.style.color = "lightpurple"; } else { testChild.style.color = "black"; } } } } window.onLoad = key();

嘗試讓所有細胞像

let cells = document.querySelectorAll('table td');

然后迭代它們

cells.forEach(cell => {...})

暫無
暫無

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

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