代码

///合并表头
//
/table:要操作的表格
//
/startIndex:要合并的单元格起始索引
//
/startIndex:要合并的单元格结束索引
//
/colCombine:要合并的列数
function CombineTableHeader(table, startIndex, endIndex,colCombine) {
    
var tb = table;
    
var newRow = tb.insertRow(0);

    
//i:插入的单元格索引,k:当前操作的单元格游标,n:表头行的最大单元格数目
    for (var i = 0, k = 0, n = tb.rows[1].cells.length; i < n; i++) {
        
var newCell = newRow.insertCell(i);
        
        newCell.style.backgroundColor
="#E0E0E0";
        newCell.style.textAlign
="center";
        newCell.style.fontWeight
="bold";
        newCell.style.height
="37px";
        
        
if (k >= startIndex && k <= endIndex) {
            
var text = tb.rows[1].cells[k].innerText;
            tb.rows[
0].cells[i].innerText = text.split("_")[0]
            tb.rows[
0].cells[i].colSpan = colCombine;

            
for (var x = 0; x < colCombine; x++) {
                tb.rows[
1].cells[k].innerText = tb.rows[1].cells[k].innerText.split("_")[1];
                k
++;
            }
            n
=n-colCombine+1;
        }
        
else {
            newCell.innerText 
= tb.rows[1].cells[k].innerText;
            tb.rows[
1].deleteCell(k);            
            newCell.rowSpan 
= 2;
            startIndex
--;
            endIndex
--;

        }
    }

}

a b_c b_d e
1 2 3 4

 

结果

a b e
c d
1 2 3 4

相关文章:

  • 2020-09-29
  • 2022-12-23
  • 2021-08-03
  • 2021-10-20
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-10
  • 2021-10-08
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案