Changing color of text background based on conditional statement
I would like to be able to write a script that changes the background text color based on a conditional statement. The statement is based on matching two numbers. If the numbers match, then the background text color should be green. If the numbers do not match, then the background color should be red. I currently have the following script:
<script>
function myFunction(tBox)
{
var sum=0;
var elts = document.getElementsByTagName('input');
for (var i=0; i<elts.length; i++)
{
var elt=elts[i];
if ( elt.id && elt.id.indexOf('hous')===0) sum += Number(elt.value);
}
document.getElementById('sum').innerText = sum;
}
</script>
This script simply sums together all of the text inputs with the prefix 'hous' and the number that concerns this question is the actual outputted sum of this function, which is outputted with:
<p id="sum"></p>
The sum output is what needs to be highlighted either red or green based on the conditional statement. The number I would like to compare this sum to is a result of this statement:
<?php echo ($_POST["qty"])*$qtyHous; ?>
Where "qty" is a text input and $qtyHous is a scaling factor. So, my question is:
How do I write the javascript that will compare these two numbers in a way that makes the text background color behind the sum turn green (the two numbers are equal) or red (the two numbers are not equal)?
你可以像这样使用style.backgroundColor
组件来访问背景颜色
if(sum != x)
document.getElementById('sum').style.backgroundColor = 'red';
链接地址: http://www.djcxy.com/p/70510.html
下一篇: 基于条件语句更改文本背景的颜色