根据第一列条件合并表格中后面相同的列

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="tab">
   
<tr>
       
<td>张三</td>
       
<td></td>
       
<td>22</td>
       
<td>数学</td>
       
<td>90</td>
   
</tr>
   
<tr>
       
<td>张三</td>
       
<td></td>
       
<td>22</td>
       
<td>语文</td>
       
<td>70</td>
   
</tr>
   
<tr>
       
<td>张三</td>
       
<td></td>
       
<td>22</td>
       
<td>英语</td>
       
<td>60</td>
   
</tr>
   
<tr>
       
<td>李四</td>
       
<td></td>
       
<td>19</td>
       
<td>数学</td>
       
<td>60</td>
   
</tr>
   
<tr>
       
<td>李四</td>
       
<td></td>
       
<td>19</td>
       
<td>语文</td>
       
<td>60</td>
   
</tr>
   
<tr>
       
<td>王五</td>
       
<td></td>
       
<td>19</td>
       
<td>英语</td>
       
<td>60</td>
   
</tr>
</table>
<script>
window.onload
=function(){
   
var tab = document.getElementById("tab");
   
var array = new Array();
   
while((e = tab.rows[0]) != null){
       
var json = {"name":e.cells[0].innerHTML, "sex":e.cells[1].innerHTML, "age":e.cells[2].innerHTML,
               
"course":e.cells[3].innerHTML, "score":e.cells[4].innerHTML};
        array.push(json);
        tab.deleteRow(
0);
    }

   
var row = tab.insertRow(-1);
   
var cell = row.insertCell(-1);
    cell.innerHTML
= array[0].name;
    cell
= row.insertCell(-1);
    cell.innerHTML
= array[0].sex;
    cell
= row.insertCell(-1);
    cell.innerHTML
= array[0].age;
    cell
= row.insertCell(-1);
    cell.innerHTML
= array[0].course;
    cell
= row.insertCell(-1);
    cell.innerHTML
= array[0].score;
   
var first = 0;
   
var rowspan = 1;
   
for(var i = 1; i < array.length; i++){
       
if(array[i].name == array[i - 1].name
           
&& array[i].sex == array[i - 1].sex
           
&& array[i].age == array[i - 1].age ){
            rowspan
++;

            row
= tab.insertRow(-1);
            tab.rows[first].cells[
0].rowSpan = rowspan;
            tab.rows[first].cells[
1].rowSpan = rowspan;
            tab.rows[first].cells[
2].rowSpan = rowspan;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].course;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].score;
        }
else{
            first
= i;
            rowspan
= 1;

            row
= tab.insertRow(-1);
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].name;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].sex;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].age;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].course;
            cell
= row.insertCell(-1);
            cell.innerHTML
= array[i].score;
        }
    }
}
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值