我有这个 HTML 表格和 JS
var example = document.getElementid('*Numerical Value*');
var exampledbref = firebase.database().ref().child('example').child('value');
exampledbref.on('value'
snap => expample.innerText = snap.val());
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<table width="200" border="1">
<tbody>
<tr>
<td>String</td>
<td>String</td>
</tr>
<tr>
<td>*Numerical Value*</td>
<td>*Numerical Value*</td>
</tr>
<tr> </tr>
</tbody>
</table>
</body>
</html>
该模型可以工作并且需要执行以下操作:从 Firebase 获取数据并将其显示在表格上。
我的问题, 如何根据从 firebase 获取的值的数量更改值的颜色?
例如,我正在考虑一个参数,如果该值小于5
,则该值本身的颜色将更改为红色。现在,如果大于 5
,则该值的颜色为绿色
。
如果正在获取的数据的值为4
,如何更改才能使该值显示为红色
,而且如果它大于 5
值的颜色变成绿色
?我怎样才能做到这一点?
最佳答案
检查值的 jQuery 解决方案是
jQuery
// for each <td>
$('td').each(function(){
// get the text value in each <td>
var val = $(this).text();
// convert to int
var int = parseInt(val);
// if more than 5
if(int > 5) {
$(this).css({'background': 'green'});
}else {
$(this).css({'background': 'red'});
}
});
一个简单的 fiddle - https://fiddle.jshell.net/Jim_Miraidev/akcgabug/
关于javascript - 如何根据值的金额更改值的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315403/