网上找了好久才找到,在这记录下。
1 <script type="text/javascript" > 2 function MergerRow(ID) { 3 var tab = document.getElementById(ID); 4 var maxCol = 6, val, count, start; //maxCol:合并单元格作用到多少列 5 for (var col = maxCol - 1; col >= 0; col--) { 6 count = 1; 7 val = ""; 8 for (var i = 0; i < tab.rows.length; i++) { 9 if (tab.rows[i].cells.length == 0 || tab.rows[i].cells[0].tagName.toLowerCase() == 'th') { 10 continue; 11 } 12 13 if (val == tab.rows[i].cells[col].innerHTML) { 14 count++; 15 } else { 16 if (count > 1) { //合并 17 start = i - count; 18 tab.rows[start].cells[col].rowSpan = count; 19 for (var j = start + 1; j < i; j++) { 20 tab.rows[j].cells[col].style.display = "none"; 21 } 22 count = 1; 23 } 24 val = tab.rows[i].cells[col].innerHTML; 25 } 26 } 27 if (count > 1) { //合并,最后几行相同的情况下 28 start = i - count; 29 tab.rows[start].cells[col].rowSpan = count; 30 for (var j = start + 1; j < i; j++) { 31 tab.rows[j].cells[col].style.display = "none"; 32 } 33 } 34 } 35 36 } 37 </script>