简体   繁体   中英

How to change column cell color of a table based on other column value in JavaScript?

I have the following table.

<table id="Alter" class="table">
    <thead>
        <tr>
            <th>Type</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="Iss in Recent">
            <td id='Tra'>{{Iss.Type}}</td>
            <td id='Qut' style="text-align: right;">{{Iss.Quantity | number : fractionSize}}</td>
        </tr>
    </tbody>
</table>

I want to change the color of Quantity cell based on type value. IF Type="some value" Color(Quantity)='red'.
I am having trouble getting the cell values.
I tried this to get the values:

var table1 = document.getElementById('Alter');

for (var i = 0; i < table1.tBodies.length; i++) {

    var rows = table1.tBodies[i].rows;
    for (var j = 0; j < rows.length; j++) {

        var cells = rows[j].cells;
        for (var k = 0; k < cells.length; k++) {
            console.log(cells[k]);
        }
    }
}

But this isnt working. I am using JavaScript. Any suggestions?

只需使用cells[k].innerText即可获取标签内的文本

为了更好的兼容性,应该使用cells[k].textContent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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