gpt4 book ai didi

jquery - 可变颜色不根据值改变

转载 作者:行者123 更新时间:2023-11-28 01:24:43 24 4
gpt4 key购买 nike

我正在尝试根据值将颜色分配给 HTML 变量。从 JSON 中可以很好地选取值,但颜色没有改变。

下面是调用变量并赋值的脚本:

<script>
var xmlcurrgct = new XMLHttpRequest();
var urlcurrgct = "../json/currgct.json";
xmlcurrgct.onreadystatechange = function() {
if (xmlcurrgct.readyState == 4 && xmlcurrgct.status == 200) {
console.log("xmlcurrgct.responseText", xmlcurrgct.responseText);
var obj1 = JSON.parse(xmlcurrgct.responseText);
console.log("obj1", obj1);
document.getElementById("currgct").innerHTML = obj1.text.currgct;
}
}
xmlcurrgct.open("GET", urlcurrgct, true);


var color;
switch (currgct) {
case "UP":
color = "yellow";
break;
case "DOWN":
color = "red";
break;
};
xmlcurrgct.send();;
</script>

我不确定我遗漏了什么。该变量可以有 2 个值 UPDOWN

JSON:
{"text":{"currgct":"UP"}}

谁能帮我找出我哪里出错了?

最佳答案

试试这个:

var xmlcurrgct = new XMLHttpRequest();
var urlcurrgct = "../json/currgct.json";
xmlcurrgct.onreadystatechange = function () {
if (xmlcurrgct.readyState == 4 && xmlcurrgct.status == 200) {
console.log("xmlcurrgct.responseText", xmlcurrgct.responseText);
var obj1 = JSON.parse(xmlcurrgct.responseText);
console.log("obj1", obj1);
document.getElementById("currgct").innerHTML = obj1.text.currgct;

// You need to wait for the asynchronous response.
var color;
switch (obj1.text.currgct) {
case "UP":
color = "yellow";
break;
case "DOWN":
color = "red";
break;
};

document.getElementById("currgct").setAttribute("style", "color: " + color + ";");
}
}
xmlcurrgct.open("GET", urlcurrgct, true);
xmlcurrgct.send();

关于jquery - 可变颜色不根据值改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32578766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com