是否可以使用 setAttribute 更改文本的颜色? 这段代码做了我要求它做的,它打印了文本
"<font color=\"08870e\">Condition true</font>"
但是我希望它能识别出我正在添加“字体”容器。
这是代码。
if(condition) {
$("status").set("text", "<font color=\"08870e\">Condition true</font>");
}
else
{
$("status").set("text", "<font color=\"03i9ie\">Condition false</font>");
}
if(condition2)
{
$("status2").set("text", "Condition 2 is true");
}
else
{
$("status2").set("text", "Condition 2 is false");
顺便说一句,它位于每 15 秒调用一次的函数中,因此不推荐使用其他方法。
更新: $ = document.getElementById
最佳答案
你需要这样的东西:
$("status").text("Condition is true").css("color", "green");
$("status").text("Condition is false").css("color", "red"); // or .css("color", "#ff0000");
您可以使用十六进制 rgb 值 "#ff0000"
"red"
这样的命名颜色
更新: 如果 $ = document.getElementById
那么这是一个 JS 方式:
$("status").innerText = "Condition is false"; // .textContent for anything other than IE < 9 and non IE browsers.
$("status").style.color = "#ff0000";
抱歉假设 $ 是 jQuery 的一部分。从此处删除标签 jQuery 并编辑问题。
关于javascript - setAttribute 和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6802998/