简体   繁体   English

如何在条件语句下更改单元格的颜色?

[英]How do I change the color of a cell under a conditional statement?

I have been working on changing the background color of individual cells within a column using a conditional statement like the example below, but I have not been able to successfully accomplish anything.我一直在努力使用如下示例中的条件语句更改列中单个单元格的背景颜色,但我无法成功完成任何事情。 I know it's probably setup incorrectly and I've never used JavaScript before.我知道它可能设置不正确,而且我以前从未使用过 JavaScript。

document.getElementById("Table").onload = function();

Table.rows[1].cells[4].onload = function() {    
    let tableCell = var
    if (tableCell > 92) {
        document.body.style.backgroundColor = "green";
    } else if ( tableCell > 90) {
        document.body.style.backgroundColor = "orange";
    } else {
        document.body.style.backgroundColor = "red";
    }
};

Any and all help is welcome.欢迎任何和所有帮助。

Ok after getting clarification see the first part of this answer example for insight, the second part for further learning.确定后,请参阅此答案示例的第一部分以获得洞察力,第二部分进一步学习。

First you'll just change the onclick to onload on the table element.首先,您只需将onclick更改为在表格元素上onload Changed to onclick just for this example but onload will fire the method also once on your project.仅针对此示例更改为onclickonload也会在您的项目上触发该方法一次。 Go ahead and click the table in the example after hitting the "run snippet" button to see result. Go 前面点击“运行片段”按钮后单击示例中的表格以查看结果。

First you need a reference to the table object to get the HTMLTableCollection which you'll then first loop the rows, and then the subsequent cells of each row.首先,您需要引用表 object 以获取HTMLTableCollection ,然后您将首先循环行,然后是每行的后续单元格。 You grab the number value from innerHTML on each pass and use parseInt to get a number instead of a string which you can then compare via expressions in an if statement on each iteration as shown in the example.您在每次传递时从innerHTML获取数字值,并使用parseInt获取数字而不是字符串,然后您可以在每次迭代时通过 if 语句中的表达式进行比较,如示例所示。 If a number is not passed, the try and catch blocks will alert you to error in the script for correction.如果某个数字未通过, trycatch块将提醒您脚本中的错误以进行更正。 Hope this helps and best of luck in your endeavor!希望这对您的努力有所帮助并祝您好运!

 parseCells = (theTable) => { // First loop the table rows. for(let r = 0, l = theTable.rows.length; r < l; r++) { // Second loop the rows column cells. for(let c = 0, e = theTable.rows[r].cells.length; c < e; c++) { try { // Giving a random number between for innerHTML 0-100 before referencing the cell in a variable for example. theTable.rows[r].cells[c].innerHTML = Math.floor(Math.random() * 101); // Get the contents and convert to number from string and set the current cell. let currentCellNumber = parseInt(theTable.rows[r].cells[c].innerHTML), currentCell = theTable.rows[r].cells[c]; // If the contents are numbers and can be parsed via expression then change colors. if (currentCellNumber > 92) { currentCell.style.backgroundColor = 'green'; } else if (currentCellNumber > 90) { currentCell.style.backgroundColor = 'orange'; } else { currentCell.style.backgroundColor = 'red'; } } // If there was error like cell contents aren't actually numbers, or table wasn't found etc, then alert via try/catch. catch { alert('Something is wrong with the input, are you sure your table cells only have numbers?') } } } }
 td { width: 5rem; height: 5rem; text-align: center; vertical-align: middle; border: gray 1px solid; transition: background-color.35s ease; }
 <strong>Click repeatedly to watch the value/color changes</strong> <table onclick="parseCells(this)"> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> </table>

For the sake of learning, hopefully this helps further.为了学习,希望这有助于进一步。 Cheers干杯

 const tablex = document.getElementById('theTable'), clicked = document.getElementById('coordinates'); randomColor = () => { return '#'+(Math.random()*0xFFFFFF<<0).toString(16); } colorMe = (that) => { that.style.backgroundColor = randomColor(); clicked.innerHTML = that.parentNode.rowIndex + '/' + that.cellIndex; } colorCell = () => { try { const xInput = document.getElementById('theX'), yInput = document.getElementById('theY'), theCell = tablex.rows[xInput.value].cells[yInput.value]; theCell.style.backgroundColor = randomColor(); clicked.innerHTML = xInput.value + '/' + yInput.value; } catch { alert('Check your x/y buddy, there are not that many cells... 0 - 2'); } }
 td { height: 5rem; width: 5rem; border: gray 1px dotted; cursor: pointer; pointer-events: all; }
 <strong>Click a box, or define one specifically below: <br/> Most recently colored cell X/Y coordinates: <span id="coordinates" style="color:blue;font-size:130%"></span><br/> <strong>Or a specific cell at a time;</strong><br/> x: <input id="theX" type="number" maxlength="1"> y: <input id="theY" type="number" maxlength="1"> <button type="button" onclick="colorCell()">Color it</button> <br/> <table id="theTable"> <tr> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> </tr> <tr> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> </tr> <tr> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> <td onclick="colorMe(this)"></td> </tr> </table>

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

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