javascript - 根据计算的小时数更改某些字体颜色 - JavaScript
问题描述
我正在根据进入和退出的时间计算停车罚单。我一直在更改颜色设置。因此,如果车辆停放 > 23 小时,字体颜色将变为“红色”,如果车辆停放 < 1 小时,字体颜色将变为“蓝色”。
我试图对每一步和我的方法添加评论。
let row = document.querySelector("#pricerow");
if (duration > 1) {
var price = (duration * 2.99).toFixed(2);
} // if it's more than 23 hrs, change the font color to red
else if (duration > 23) {
// change the font color to red
var price = row.style.color = "red";
} // if the vehicle was parked for less than 1 hr, change the font color to blue
else{
var price = 0;
row.style.color = "blue";
// price.style.fontcolor = 'blue' // not working
// price.fontcolor("blue");
}
tb.innerHTML += `<tr>
<td>${car.license}</td>
<td id='pricerow'>${price}</td>
<td>${duration}</td>
<td>${timeIn}</td>
<td>${timeOut}</td>
</tr>`
});
}
解决方案
只需使用. 设置CSS 颜色属性.style.color
即可。
var row = document.querySelector("#pricerow");
if (duration > 1) {
} // if it's more than 23 hrs, change the font color to red
else if (duration > 23) {
// change the font color to red
row.style.color = "red";
} // if the vehicle was parked for less than 1 hr, change the font color to blue
else{
row.style.color = "blue";
}
推荐阅读
- html - 在内容底部对齐页脚,推到侧边栏的右侧
- java - DialogFragment 创建和调用的问题
- c - 为什么 glibc strcspn() 可以安全地访问显然超过字符串末尾的内存?
- r - R 图形 - 选择轴
- string - Deleting a string between two carriage returns tsql
- excel - 将数据从数据透视表提取到 VBA 中的数组(Excel)
- java - 我可以从多个表中获取数据而没有可连接或外键吗
- java - 如何正确拆分字符串?
- ruby - 如何将 ruby-watir 代码放入定义的方法中?
- service-worker - Why service-worker does not cache js file