简体   繁体   English

如果数据动态变化,如何修复 HTML 中表行的背景颜色(静态)?

[英]How to fix background-color(static) of table rows in HTML if the data changes dynamically?

I have an order table, where orders are going in and out, and I have to fix background-color of entire rows( I mean in every three lines the background color is changing in a loop).我有一个订单表,订单进出,我必须修复整行的背景颜色(我的意思是每三行背景颜色在一个循环中改变)。 For example:例如:

#orders tr:nth-child(3n+3) {
    background: #e7eff6;
}

#orders tr:nth-child(3n+4) {
    background: #d0e1f9;
}
    
#orders tr:nth-child(3n+2) {
    background: #adcbe3;
}

But there's a problem, because if an order is going in or an order is being canceled and going out, then the rows background-color is going to change, but I have to fix it and remain the color what firstly got.但是有一个问题,因为如果一个订单进入或者一个订单被取消和退出,那么行背景颜色将会改变,但我必须修复它并保持最初得到的颜色。

So, I need to separate rows, not necessary by background-colors, but it must to be fixed if the data is changing dynamically.所以,我需要分隔行,这不是背景颜色所必需的,但如果数据动态变化,则必须修复它。

Does anybody have an idea?有人有想法吗?

You can use modulus % in JS for setting background color for rows once on load.您可以在 JS 中使用模数%在加载时为行设置背景颜色。 For example if your table have id=tableId , you can run something like that例如,如果您的表有id=tableId ,您可以运行类似的东西

let tableEl = document.getElementById("tableId");
let index = 0;
let colors = ["#e7eff6","#d0e1f9","#adcbe3"]
for (let trEl of tableEl.rows) {
    trEl.style.backgroundColor = colors[index % colors.length];
    index++;
}

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

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