根据TD更少或更多的值在表的td上添加css类

时间:2016-05-25 08:48:52

标签: html css html-table

我有一个html表,里面有两个td内容。这是一个动态的TD。我想实现像每个表格行中的功能,每个大的td值文本颜色显示为绿色,较少值的文本显示为红色;

请找到我的源代码:

forData+='<tr><td>'+forPlant+'</td><td>'+forAsking+'</td><td>'+forProduction+'</td></tr>';
                $(".forTable").append(forData);
                if(forAsking<forProduction){
                    console.info("Less value");
//-----here I want to display Whatever value is less it displayed as red and other is green
                }else{
  //---same manner followed in there as well
                    console.info("More Asking");
                }

1 个答案:

答案 0 :(得分:1)

为这些颜色创建2个类

.green{
color:green;
}

.red{
color:red;
}

然后首先比较这些值然后追加结果。

forData+='<tr><td>'+forPlant+'</td>';

if(forAsking<forProduction){

                   forData+='<td class="green">'+forProduction+'</td><td class="red">'+forAsking+'</td>';

                }else{

                    forData+='<td class="green">'+forAsking+'</td><td class="red">'+forProduction+'</td>';
                }

forData+= '</tr>';

 $(".forTable").append(forData);

You can see the demo here

相关问题