我正在嘗試使用 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>
uj5u.com熱心網友回復:
將您的條件添加到回應中。就像是:
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;');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338691.html
標籤:javascript html css 阿贾克斯 文字颜色
上一篇:動態更新網頁上的Div(燒瓶)
