问题
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