简体   繁体   English

如果值小于另一个,请使用Javascript更改表格单元格的颜色

[英]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.

相关问题 如果一个值小于另一个值,则突出显示该值 - Highlight a value if its less than another value 根据表格单元格的值更改其背景颜色(由XML导入) - Change background color of a table cell based on its value(Imported by XML) 如何使用javascript根据单元格的值更改背景色? - How to change the background color of a cell based on its value using javascript? 使用JavaScript根据JSON值更改表格单元格背景色 - Using javascript to change table cell background color based on json value 如果数量列的数值小于10,如何更改表格行的背景颜色 - How to change background color of table row if quantity column has numeric value less than 10 如何使用javascript根据另一个单元格上的单选按钮更改表格单元格颜色? - How to change a table cell color based on radio button on another cell with javascript? 根据单元格值在3种不同背景颜色之间切换,但前提是另一个调用值与 - change between 3 different background color based on cell value but only if another call value is different than 如何在javascript中更改表格单元格的颜色 - How to change the color of a cell of a table in javascript 单击时如何动态更改表单元格中的链接值时表格行的背景色 - how to dynamically change a table row background color when a link value within its cell changes when clicked 我可以使用JavaScript / AngularJS使表格单元格中的数字基于其值具有不同的颜色 - Can I make a number in a table cell have a different color based on its value using JavaScript/AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM