[英]Use Javascript to change a table cell color if its value is less than another
Can't figure out what I'm doing wrong here: 在这里找不到我在做什么错:
Just want cell .tt
to be red when its numerical values is less than cell dd
只希望单元格.tt
的数值小于单元格dd
时为红色
HTML: HTML:
<table class="colorMe">
<tr><td class="tt">2000</td><td>3500</td></tr>
<tr><td>3000</td><td>2500</td></tr>
<tr><td id="dd">4000</td><td>4500</td></tr>
</table>
JS: JS:
$(".colorMe .tt" ).each(function() {
var val = parseInt(this.innerHTML, 10);
if (val < document.getElementById("dd");) {
this.style.backgroundColor = "#F00000";
}
});
No idea why this isn't working. 不知道为什么这不起作用。
You have to get both values as you did the first. 您必须像第一个一样获得两个值。
$('.colorMe .tt').each(function() {
var val = parseInt( $(this).text(), 10),
dd = parseInt( $('#dd').text(), 10);
if (val < dd) {
$(this).css('background-color', 'red');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.