根据值更改跨度标签的颜色
问题描述:
我想在以下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>