简体   繁体   English

根据值更改跨度标签的颜色

[英]Change color of span tag depending on value

I want to change the color of the message variable in the following HTML: 我想在以下HTML中更改message变量的颜色:

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

I am using Jinja2 together with Flask - Python to pass a value to the {{ message }} variable. 我将Jinja2与Flask-Python结合使用,将值传递给{{ message }}变量。 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;");
            }
});

The result of the document.getElementById('tex').value is always undefined and the color of the text of the message variable is always red. document.getElementById('tex').value的结果始终是undefined的, message变量文本的颜色始终为红色。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM