根据值更改跨度标签的颜色 [英] Change color of span tag depending on value

查看:67
本文介绍了根据值更改跨度标签的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下HTML中更改message变量的颜色:

I want to change the color of the message variable in the following HTML:

<span id="mess" style="visibility:visible;">
                <text id="tex">{{ message }}</text>
</span

我将Jinja2与Flask-Python结合使用,将值传递给{{ message }} 多变的.这是我尝试执行的操作:

I am using Jinja2 together with Flask - Python to pass a value to the {{ message }} variable. Here is how I tried to do it:

$(document).ready(function(){

            if (document.getElementById('tex').value == 'Message sent !')
            {
                document.getElementById('tex').setAttribute("style", "color:green;");
            }
            else
            {
                document.getElementById('tex').setAttribute("style", "color:red;");
            }
});

document.getElementById('tex').value的结果始终为undefined,并且message变量的文本的颜色始终为红色.

The result of the document.getElementById('tex').value is always undefined and the color of the text of the message variable is always red.

有没有办法可以做到这一点?预先谢谢你.

Is there a way with which I can accomplish that ? Thank you in advance.

推荐答案

让我们使用包含选择器.

Lets use contains selector.

$(document).ready(function() {
  if ($('#tex:contains("Message sent !")').length) {
    $('#tex').css('color', 'green');
  } else {
    $('#tex').css('color', 'red');
  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="mess" style="visibility:visible;">
  <text id="tex">Message sent !</span>
</span>

这篇关于根据值更改跨度标签的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆