简体   繁体   English

如何使用DOM更改表格中单行的背景颜色

[英]How to change background color of a single row inside a table with DOM

I want to modify the color of each row inside a table only if the text content inside the 4th cell is equal to "DELAYED" I maked a function to do that but no color changing... 我只想在第4个单元格内的文本内容等于“ DELAYED”的情况下修改表中每一行的颜色,所以我做了一个函数来做到这一点,但没有改变颜色。

function assignColor()
{
    //getting all the tr elements from document
    var x = document.getElementsByTagName("tr");

    for(var i = 0; i < x.length; i++)
    {
        //Accessing to text Content in the 4th cell of the tr
        if(x[i].cells[3].textContent == "DELAYED")
        {
            x[i].style.backgroundColor = "red";
        }
    }

}

i think it's something wrong with the way i populate the table from js to html 我认为将表格从js填充到html的方式有问题

function printFlights(arrayOfFlight)
{
    //I insert tr till the length of an array
    for(var i = 0; i < arrayOfFlight.length; i++)
    {

        //creating tr element
        var tr = document.createElement("tr");

        //Inserting cells inside tr using propertys of an object

        tr.insertCell(0).innerHTML = 
        formatDate(arrayOfFlight[i].arrivalDate);
        tr.insertCell(1).innerHTML = 
        formatTime(arrayOfFlight[i].arrivalDate);
        tr.insertCell(2).innerHTML = arrayOfFlight[i].destination;
        tr.insertCell(3).innerHTML = arrayOfFlight[i].status;
        tr.insertCell(4).innerHTML = arrayOfFlight[i].airplane;
        tr.insertCell(5).innerHTML = arrayOfFlight[i].gate;

        //add tr to tbody
        tableBody.appendChild(tr);
    }
}

Your code is working: 您的代码正在运行:

 function assignColor() { //getting all the tr elements from document var x = document.getElementsByTagName("tr"); for(var i = 0; i < x.length; i++) { //Accessing to text Content in the 4th cell of the tr if(x[i].cells[3].textContent == "DELAYED") { x[i].style.backgroundColor = "red"; } } } assignColor(); 
 <table> <tr> <td>a</td> <td>a</td> <td>c</td> <td>DELAYED</td> </tr> </table> 

Your code seems to work fine, with one exception - if the <td> elements in your HTML have any whitespace in them (like this demo below), then your code needs to .trim() the content of the cells before the string comparison. 您的代码似乎可以正常工作,但有一个例外-如果HTML中的<td>元素中包含空格(如下面的演示所示),则您的代码需要在字符串比较之前.trim()单元格的内容。

See below: 见下文:

 function assignColor() { var x = document.getElementsByTagName("tr"); for (var i = 0; i < x.length; i++) { if (x[i].cells[3].textContent.trim() == "DELAYED") { x[i].style.backgroundColor = "red"; } } } assignColor(); 
 <table border="1"> <tr> <td> Col 1 </td> <td> Col 2 </td> <td> Col 3 </td> <td> Col 4 </td> </tr> <tr> <td> Col 1 </td> <td> Col 2 </td> <td> Col 3 </td> <td> DELAYED </td> </tr> 

Try it this way, it will check for Delayed regardless of the cell number. 尝试这种方式,无论单元号如何,它都会检查“延迟”。

 function assignColor() { var x = document.querySelectorAll('tr td'); for(var i = 0; i < x.length; i++) { if(x[i].textContent == "DELAYED") { x[i].style.backgroundColor = "red"; } } } 
 <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>DELAYED</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>DELAYED</td> </tr> <tr> <td>1</td> <td>2</td> <td>DELAYED</td> <td>4</td> </tr> </table> 

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

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