Can I make a table cell have a different background color if the value =< a particular number with jquery or PHP?

前端 未结 5 556
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 17:31

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

5条回答
  •  庸人自扰
    2021-01-28 18:26

    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

    99101
    20050

    You can populate more class in CSS and else if statements, if you need more conditions.

    Here is the live example http://jsfiddle.net/kayadiker/DL6U2/2/

提交回复
热议问题