使用javascript向动态创建的表列添加颜色

使用javascript向动态创建的表列添加颜色,javascript,jquery,html,Javascript,Jquery,Html,MyHtml表 <table border="1" id="pay"> <thead> <th>#</th> <th>Admission No</th> <th>Student Name</th> <th>Paid Amount</th> <th>Jan </th> <th>Feb </th> <th>March &l

MyHtml表

<table border="1" id="pay">
<thead>
<th>#</th>
<th>Admission No</th>
<th>Student Name</th>
<th>Paid Amount</th>
<th>Jan </th>
<th>Feb </th>
<th>March </th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</thead>

<tbody id="payCompClass">

</tbody>
</table>
我想要的是根据fullpayment的编号在表格列中填充一种颜色,有没有一种好的方法来实现这一点。类似于此

   |#   | Admission |fullname|fullpayment |Jan|Feb|Mar|Apr|May|....|Dec|
   |0   | 1010  |abcName     |3           |red|red|red|no |no|.... |no|
   |1   | 1011  |abcName     |2           |red|red|no |no |no|.... |no|     
   |2   | 1012  |abcName     |1           |red|no |no |no |no|.... |no|                                                     
   |3   | 1013  |efgName     |1           |red|no |no |no |no|.... |no|
我在trHtml代码的末尾尝试了一个for循环,但不起作用,有人能提出解决方案吗, 多谢各位


var students=[{stuid:'1',stu_name:'A',fullpayment:'3'},
{stuid:'2',stu_name:'B',fullpayment:'4'},
{stuid:'3',stu_name:'C',fullpayment:'2'},
{stuid:'4',stu_name:'D',fullpayment:'1'}];
$。每个(学生,功能(i,项目){
var$tr=$('');
$tr.append($('',{text:(i+1)}));
$tr.append($('',{text:item['stuid']}));
$tr.append($('',{text:item['stu_name']}));
$tr.append($('',{text:item['fullpayment']}));
$tr.append($('',{text:'JAN'}));
$tr.append($('',{text:'FEB'}));
$tr.append($('',{text:'MAR'}));
$tr.append($('',{text:'APR'}));
$tr.append($('',{text:'MAY'}));
$tr.append($('',{text:'JUN'}));
$tr.append($('',{text:'JUL'}));
$tr.append($('',{text:'AUG'}));
$tr.append($('',{text:'SEP'}));
$tr.append($('',{text:'OCT'}));
$tr.append($('',{text:'NOV'}));
$tr.append($('',{text:'DEC'}));
var j=1;

虽然(j您希望在12个月内检查全额付款,所以请在付款月结束时循环检查

 $.each(data["students"],function(i,item){
    var html = "<tr>";
    html += "<td>"+i+"</td><td>"+item.stuid+"</td><td>"+item.stu_name+"</td>"+
            "<td>"+item.fullpayment+"</td>";
    //checking payment month
    for(var i = 0;i < 12;i++) {
        if(i < item.fullpayment) {
            html += "<td bgcolor='red'></td>";
        } else {
            html += "<td>no</td>";
        }
    }
    $("tbody").append(html+"</tr>");
 });
$。每个(数据[“学生]),函数(i,项){
var html=“”;
html+=“”+i+“”+item.stuid+“”+item.stu\u name+“”+
“+项目。全额付款+”;
//每月支票付款
对于(变量i=0;i<12;i++){
如果(i<项目全额付款){
html+=“”;
}否则{
html+=“否”;
}
}
$(“tbody”).append(html+);
});
   |#   | Admission |fullname|fullpayment |Jan|Feb|Mar|Apr|May|....|Dec|
   |0   | 1010  |abcName     |3           |red|red|red|no |no|.... |no|
   |1   | 1011  |abcName     |2           |red|red|no |no |no|.... |no|     
   |2   | 1012  |abcName     |1           |red|no |no |no |no|.... |no|                                                     
   |3   | 1013  |efgName     |1           |red|no |no |no |no|.... |no|
 $.each(data["students"],function(i,item){
    var html = "<tr>";
    html += "<td>"+i+"</td><td>"+item.stuid+"</td><td>"+item.stu_name+"</td>"+
            "<td>"+item.fullpayment+"</td>";
    //checking payment month
    for(var i = 0;i < 12;i++) {
        if(i < item.fullpayment) {
            html += "<td bgcolor='red'></td>";
        } else {
            html += "<td>no</td>";
        }
    }
    $("tbody").append(html+"</tr>");
 });