Change color of span tag depending on value

拈花ヽ惹草 提交于 2020-05-19 03:57:28

问题


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

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;");
            }
});

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.


回答1:


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>


来源:https://stackoverflow.com/questions/47663957/change-color-of-span-tag-depending-on-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!