[英]How do I conditionally change the colour of my table row in HTML?
I have a table where the background colour of one of the rows needs to be changed dynamically.我有一张表,其中一行的背景颜色需要动态更改。 For example, if the data is greater than 5, the colour should be green, else it should be red.
例如,如果数据大于 5,则颜色应为绿色,否则应为红色。
can be done with css (if the structure fits)可以用 css 完成(如果结构合适)
td { border: 1px solid ; padding: 5px; position: relative; width: 50px; } .item { position: relative; z-index: 2; } .bg { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } .item:nth-child(1) ~ .bg { background: blue; } .item:nth-child(2) ~ .bg { background: darkblue; } .item:nth-child(3) ~ .bg { background: red; } .item:nth-child(4) ~ .bg { background: darkgreen; } .item:nth-child(5) ~ .bg { background: darkorange; }
<table> <tr> <td> <div class="item">1</div> <div class="bg"></div> </td> <td> <div class="item">1</div> <div class="item">2</div> <div class="bg"></div> </td> </tr> <tr> <td> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="bg"></div> </td> <td> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="bg"></div> </td> </tr> <tr> <td colspan="2"> <span class="item">1</span> <span class="item">2</span> <strong class="item">3</strong> <i class="item">4</i> <div class="item">5</div> <div class="bg"></div> </td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.