Ajax - 根据 return 值更改文本颜色

Ajax - change text color based on return value

我正在尝试使用 html/ajax 显示传感器值。到目前为止,这按预期工作,但是我希望文本颜色根据值进行更改。 例如

if value < 50 then fontcolor= blue 
if value >49 then fontcolor = red

这可能吗?

<p style="color:blue; position: absolute; top: 810px; width: 100px; padding-left: 340px;" id="ofen_VL">0</p>
<script>
setInterval(function() {
  // Call a function repetatively with 10 Second interval
getData();
}, 10000); //10sec

function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ofen_VL").innerHTML =
  this.responseText;
}
 };
xhttp.open("GET", "ofen_VL.txt", true);
xhttp.send();
}

</script>

将您的情况添加到回复中。类似于:

if (this.readyState == 4 && this.status == 200) {
  document.getElementById("ofen_VL").innerHTML = this.responseText;
  if (Number(this.responseText) > 50) {
    document.getElementById("ofen_VL").setAttribute('style', 'color: blue;');
  }
  else {
    document.getElementById("ofen_VL").setAttribute('style', 'color: red;');
  }
}