首页 > 解决方案 > 根据计算的小时数更改某些字体颜色 - 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>`

  });

}

标签: javascript

解决方案


只需使用. 设置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";
}

推荐阅读