Change colour of table cells depending on value

混江龙づ霸主 提交于 2019-12-02 19:24:01

问题


I'm using jQuery to edit the background-color of table cells. My code is as follows (the each cell has numbers in the format "x/y" so I mine them out at the start):

        $(document).ready(function(){
            $("#overview td").click(function(event){

                var content = $(this).html();
                var vals = content.split("/");
                var ratio = vals[0]/vals[1];
                alert(ratio);


                var red;
                var green;

                if(vals[1] == 0){
                    $(this).css('background-color', '#00FF00');
                } else{
                    if(ratio > 0.5){
                        red = 255;
                        green = parseInt(-2*255*ratio+(2*255));
                    } else{
                        green = 255;
                        red = parseInt(2*255*ratio);
                    }   
                    var rgbColor = 'rgb(' + red + ',' + green+ ', 0)';
                    var hexColor = rgb2hex(rgbColor);
                    $(this).css('background-color', hexColor);
                }
            });
        });

Now this works when I click on each individual cell, but I would like to colour all of the cells on $(document).ready(). I think the .each() method may be what I'm looking for, but I can't figure out how to make it work properly...

Any help would be greatly appreciated!


回答1:


Just as Jason P mentioned the following code should work fine :

        $(document).ready(function () {
            $("#overview td").each(function () {

                var content = $(this).html();
                var vals = content.split("/");
                var ratio = vals[0] / vals[1];
                alert(ratio);


                var red;
                var green;

                if (vals[1] == 0) {
                    $(this).css('background-color', '#00FF00');
                } else {
                    if (ratio > 0.5) {
                        red = 255;
                        green = parseInt(-2 * 255 * ratio + (2 * 255));
                    } else {
                        green = 255;
                        red = parseInt(2 * 255 * ratio);
                    }
                    var rgbColor = 'rgb(' + red + ',' + green + ', 0)';
                    var hexColor = rgb2hex(rgbColor);
                    $(this).css('background-color', hexColor);
                }
            });
        });

The .each method is just simply an iteration over the retrieved objects.




回答2:


Thanks Nunners, there were header values that broke the code. I've fixed it using the following if block:

if(vals[1] == undefined){
    return true;
} else{
    //change colour
}



回答3:


Just trigger click event:

$(document).ready(function() {
    $("#overview td").click(function (event) {
        // ...
    })
    .trigger('click');
});


来源:https://stackoverflow.com/questions/18992382/change-colour-of-table-cells-depending-on-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!