简体   繁体   English

如何有条件地更改 HTML 中表格行的颜色?

[英]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.

相关问题 如何更改有序列表 (HTML) 中项目符号的颜色? - How do I change the colour of a bullet in an ordered list (HTML)? 如何有条件地格式化我的 HTML 表格? - How can I conditionally format my HTML table? 如何在不使用color属性的情况下更改文本的颜色? (HTML和CSS) - How can I change the colour of my text without using the colour attribute? (HTML & CSS) 如果知道单元格的行和列索引,如何更改HTML表单元格中的属性? - How do I change an attribute in an HTML table's cell if I know the row and column index of the cell? 如何更改下拉菜单的背景颜色? - How do I change the background colour of a dropdown? 如何从html表格的最后一行删除底部边框? - How do I remove the bottom border from the last row in my html table? HTML电子邮件-如何获取2行4列的表格以正确呈现Outlook 2007-2016 - HTML Email - How do I get my 2 row, 4 column table to render properly Outlook 2007 - 2016 如何使用JavaScript根据内容更改表格行颜色 - How to change Table row colour based on content using Javascript 如何防止背景色/图像出现在上一行(引导程序) - How do I prevent my background colour/image from appearing in the previous row (bootstrap) 如何使用HTML和CSS更改两个/多个不同字段集的背景颜色? - How do I change the background colour of two/more different fieldsets using HTML and CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM