admin管理员组文章数量:1410730
I have an HTML table with a lot of numbers.
Is it possible to have a table cell change background color if the value inside that cell (or column) equals or is less than a particular number?
For example: if cell =< "3000", background-color=#FF0000
Is there a way to make this work in reality?
Erik
I have an HTML table with a lot of numbers.
Is it possible to have a table cell change background color if the value inside that cell (or column) equals or is less than a particular number?
For example: if cell =< "3000", background-color=#FF0000
Is there a way to make this work in reality?
Erik
Share Improve this question edited Sep 15, 2011 at 4:29 JohnFx 34.9k18 gold badges107 silver badges166 bronze badges asked Sep 15, 2011 at 4:05 ErikErik 5,79127 gold badges74 silver badges120 bronze badges 2- You are going to need more tags. Do you want to do this with client side script or server side script? What technology are you using. Right now the only way to answer your question is "Yes. You can do that." – JohnFx Commented Sep 15, 2011 at 4:07
- Your cell value can changes after page loading or value always is static during all page life? In other words: you need to dinamically change background of cell or background can be applied only once? – Andrew D. Commented Sep 15, 2011 at 5:47
5 Answers
Reset to default 2Here's an example of how to do it with JS/Jquery
$("#Yourtable td").each( function() {
var thisCell = $(this);
var cellValue = parseInt(thisCell.text());
if (!isNaN(cellValue) && (cellValue <=3000)) {
thisCell.css("background-color","#FF0000");
}
}
)
Try this one
var table = document.getElementById("tableId");
var row = table.rows[rowIndex];
var cell = row.cells[cellIndex];
var cellValue = Number(cell.innerHTML);
if (cellValue > 3000)
cell.style.backgroundColor = "red";
Yes it is easy to do this sort of thing.
For example, with jQuery, something like this:
$('table.mytable td').each(function() {
if (this.textContent <= 3000) {
$(this).css('background', '#FF0000');
}
}
You could use code like this:
$(".colorMe td").each(function() {
var val = parseInt(this.innerHTML, 10);
if (val < 3000) {
this.style.backgroundColor = "#F00000";
}
});
See a working example here: http://jsfiddle/jfriend00/GAwrB/.
Here is the example //CSS
.lowerthan100{
background-color:red;
color:white;
}
.higherthan100{
background-color:white;
color:red;
}
//JAVASCRIPT
$('#mytable td').each(function() {
if(parseInt($(this).html())>100){
$(this).addClass("higherthan100");
}else if (parseInt($(this).html())<100){
$(this).addClass("lowerthan100");
}
});
//HTML
<table border="1px" id="mytable">
<tr><td>99</td><td>101</td></tr>
<tr><td>200</td><td>50</td></tr>
</table>
You can populate more class
in CSS and else if
statements, if you need more conditions.
Here is the live example http://jsfiddle/kayadiker/DL6U2/2/
本文标签:
版权声明:本文标题:javascript - Can I make a table cell have a different background color if the value =< a particular number with jquery or 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744854394a2628688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论