1.函数功能描述
根据table第一列的值进行合并:将相邻的有相同值的单元格进行合并
2.效果
3.JS代码
<script type="text/javascript">
mc('table',0,0,0);
//合并单元格
//table1:代表table的id值
//startRow:列的开始行
//endRow:列的结束行
//col:列号 合并相同内容的行
function mc(table1, startRow, endRow, col) {
var tb = document.getElementById(table1);
if (col >= tb.rows[0].cells.length) {
return;
}
if (col == 0) { endRow = tb.rows.length-1; }
for (var i = startRow; i < endRow; i++) {
if (tb.rows[startRow].cells[col].innerHTML == tb.rows[i + 1].cells[0].innerHTML) {
tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);
tb.rows[startRow].cells[col].rowSpan = (tb.rows[startRow].cells[col].rowSpan | 0) + 1;
if (i == endRow - 1 && startRow != endRow) {
mc(table1, startRow, endRow, col + 1);
}
} else {
mc(table1, startRow, i + 0, col + 1);
startRow = i + 1;
}
}
}
</script>