简体   繁体   English

如何使用 HTML、CSS 和 JavaScript 根据值更改行的背景颜色?

[英]How can I change the background color of a row based on the value using HTML, CSS and JavaScript?

I'm completely new in HTML, CSS and JavaScript.我对 HTML、CSS 和 JavaScript 完全陌生。

I'm trying to do a table that background color of the row changes based on the value of the last two columns (or the "td" on the table).我正在尝试制作一个表格,该表格根据最后两列的值(或表格上的“td”)更改行的背景颜色。

1 1

If the value in the column "Nota Final" >= 7 or "Frequência" >= 75, so the background color of this row changes to green, else, it changes to red.如果“Nota Final”列中的值 >= 7 或“Frequência” >= 75,则该行的背景颜色变为绿色,否则变为红色。

I want this output:我想要这个输出:

2 2

Here is my code.这是我的代码。 I started to study it this week, so, sorry for the messy code...我这周开始研究它,所以,抱歉乱码......

 var list1 = []; var list2 = []; var list3 = []; var list4 = []; var list5 = []; var list6 = []; var n = 1; var x = 0; function AddRow(){ var AddRown = document.getElementById('show'); var NewRow = AddRown.insertRow(n); list1[x] = n; list2[x] = document.getElementById("fname").value + " " + document.getElementById("lname").value; list3[x] = document.getElementById("grade1").value; list4[x] = document.getElementById("grade2").value; //Nota final var n1 = document.getElementById("grade1").value; var n2 = document.getElementById("grade2").value; var mean = (Number(n1)+Number(n2))/2; list5[x] = mean; list6[x] = document.getElementById("frequency").value + "%"; var cel1 = NewRow.insertCell(0); var cel2 = NewRow.insertCell(1); var cel3 = NewRow.insertCell(2); var cel4 = NewRow.insertCell(3); var cel5 = NewRow.insertCell(4); var cel6 = NewRow.insertCell(5); cel1.innerHTML = list1[x]; cel2.innerHTML = list2[x]; cel3.innerHTML = list3[x]; cel4.innerHTML = list4[x]; cel5.innerHTML = list5[x]; cel6.innerHTML = list6[x]; n++; x++; }
 #show { background-color: black; } #show th { padding: 13px; } #show td { padding: 13px; background-color: #ffffff; text-align: center; }
 <table id="show"> <thead> <tr id="title"> <th>ID</th> <th>Nome</th> <th>1° Semestre</th> <th>2° Semestre</th> <th>Nota Final</th> <th>Frequência</th> </tr> </thead> </table>

"Nota Final" >= 7 or "Frequência" >= 75 "Nota Final" >= 7 或 "Frequência" >= 75

From the sample table this should be "Nota Final" >= 7 AND "Frequência" >= 75从示例表中,这应该是"Nota Final" >= 7 AND "Frequência" >= 75

How can I change the background color of a row based on the value using HTML, CSS and JavaScript?如何使用 HTML、CSS 和 JavaScript 根据值更改行的背景颜色?

You can use an if else in Javascript for this.为此,您可以在 Javascript 中使用if else Check out this tutorial.看看这个教程。 What I would do is set the default color to red and if the 2 conditions are met, change it to green我要做的是将默认颜色设置为红色,如果满足 2 个条件,请将其更改为绿色

You can do你可以做

 if (document.getElementById("frequency").value>=75 && document.getElementById("notaFinal").value>=7){ //change background to green }

Try something like this.尝试这样的事情。 The key would be to write a function to add a row, which you've already done.关键是编写一个函数来添加一行,你已经完成了。 In that function, you can check the values for the column(s) of interest.在该函数中,您可以检查感兴趣的列的值。 Then, if it matches the goal, add a specific class (in this case, 'green') to the row you created before you add it to the table.然后,如果它符合目标,则在将其添加到表之前,将特定类(在本例中为“绿色”)添加到您创建的行。 Otherwise, add a different class ('red', in this example).否则,添加一个不同的类(在本例中为“红色”)。 Good luck!祝你好运!

 function AddRow(listOfParams){ var table = document.getElementById("show"); var row = document.createElement("tr"); for (i=0;i<6;i++){ var td = document.createElement("td"); td.innerHTML = listOfParams[i]; row.appendChild(td); } if (listOfParams[4] >= 7 || listOfParams[5] >= 75){ row.classList.add("green"); } else{ row.classList.add("red"); } table.append(row); } AddRow([1,"oliver",6.5,7.8,7.2,90]); // add one row // add multiple rows const ids = [2,3]; const nomes = ['name1','name2']; const semester1 = [4.5,3.2]; const semester2 = [3.5,4.4]; const final = [7,6]; const frequencias = [70,70]; for (row=0;row<ids.length;row++){ AddRow([ids[row],nomes[row],semester1[row],semester2[row],final[row],frequencias[row]]); }
 #show { background-color: black; } #show th { padding: 13px; color: white; } #show td { padding: 13px; text-align: center; } .green{ background-color:green !important; } .red{ background-color:red !important; }
 <table id="show"> <thead> <tr id="title"> <th>ID</th> <th>Nome</th> <th>1° Semestre</th> <th>2° Semestre</th> <th>Nota Final</th> <th>Frequência</th> </tr> </thead> </table>

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

相关问题 HTML / CSS / JavaScript:如何根据数据更改表格行的颜色 - HTML/CSS/JavaScript: How to change color of table row based on data 如何根据数据表中的单元格值更改行的背景颜色? - How Can I Change Background Color of Row based on cell value in Datatable? 如何使用javascript根据单元格的值更改背景色? - How to change the background color of a cell based on its value using javascript? 如何使用 JavaScript 更改 css 背景颜色? - how to change css background color using JavaScript? 如何更改在 javascript 中以.value 结尾的变量的背景颜色 - How can I change the background color of a variable that ends in .value in javascript 如何使用 JavaScript / jQuery 根据表中的值更改 td 背景颜色? - How can I change td background color depending on value in a table using JavaScript / jQuery? 如何使用javascript更改选择时的标签背景颜色? - How can i Change tab background color on selection using javascript? 根据字段值更改行的背景色 - Change the background color of a row based on field value 如何根据值使用Javascript更改颜色? - How to change color using Javascript based on value? 使用JavaScript根据JSON值更改表格单元格背景色 - Using javascript to change table cell background color based on json value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM