![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
///合并表头
///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 |