javascript - setAttribute 和文本

标签 javascript css

是否可以使用 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/

相关文章:

javascript - 将 material-ui 为子组件生成的样式放在为父组件生成的样式之后?

javascript - 如何使用 document.getElementById 和 getElementsByClassName 做 Jasmine 测试用例以显示无 css 属性

javascript - CSS 或 JQuery 中的特定图像扩展选择器

javascript - 每次用户输入内容时如何运行脚本?

javascript - 固定字数限制后如何更改文本颜色

javascript - AngularJS - 模块 - Controller 访问问题

javascript - 监控页面中的元素

javascript - innerHTML 遇到问题

javascript - 在 Javascript 异步函数中设置全局变量

javascript - 基于来自 JSON 的图像数组设置图像样式