简体   繁体   中英

Change style of a cell if value is zero

I found how to do it with inputs.

 input[value="0"] { background-color:#F7ECEC; color:#f00;} 

I want to do the same with table cells. Can someone help me? Thx.

You can use the following code when you get the values from db on windows.load or in success from asynchronous call:

 var table = document.getElementById("table"); var cells = table.getElementsByTagName("td"); for (var i = 0; i < cells.length; i++) { if (parseInt(cells[i].textContent, 10) === 0) { cells[i].style.background = "red"; } } 
 <table id="table"> <tr> <td>1</td> <td>0</td> <td>1</td> </tr> <tr> <td>1</td> <td>2</td> <td>0</td> </tr> </table> 

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