首页 > 代码库 > 表格代码

表格代码

/*** Created by lenovo on 2016/12/10.*/function tablecols(id,endrows) {var trs = document.getElementById(‘tab‘).getElementsByTagName(‘tbody‘)[0].rows;var count = 1;var count2 = 1;var beginrows = 0;for (var j = 0; j < endrows; j++) {if(j==0) {for (var i = 0; i < trs.length; i++) {if (i == 0) {var tds = trs[0].cells[j].innerHTML;continue;}if (trs[i].cells[j].innerHTML == tds) {++count;if (i == trs.length - 1) {if (count >= 2) {var b = i + 1;dealwith(trs, b, count, j);count = 1;}}} else {if (count >= 2) {dealwith(trs, i, count, j);count = 1;}tds = trs[i].cells[j].innerHTML;}}}else {for (var n = 0; n < trs.length;) {var rowspan = trs[n].cells[j - 1].getAttribute(‘rowspan‘);if (n == 0) {var tds = trs[0].cells[j].innerHTML;}if (rowspan) {for (var m = 0; m < rowspan; m++) {if (trs[n].cells[j].innerHTML == tds) {++count2;if(n==0){--count2}if (n == trs.length - 1) {if (count2 >= 2) {var b = n + 1;dealwith(trs, b, count2, j);count2 = 1;}}} else {if (count2 >= 2) {dealwith(trs, n, count2, j);count2 = 1;}tds = trs[n].cells[j].innerHTML;}n++;}rowspan = null;tds=null;}else{++n}}}}}function dealwith(trs,i,count,j){for (var k = i-1; k > i - count; k--) {trs[k].cells[j].style.display = "none";}trs[i - count].cells[j].rowSpan = count;}

html代码引用

<meta charset="UTF-8" /><script src="http://www.mamicode.com/table.js"></script>
张三 22 数学 90
张三 22 数学 90
张三 22 数学 90
张三 22 语文 70
张三 22 英语 60
李四 22 数学 60
李四 19 语文 60
王五 19 英语 60
王五 19 英语 60
王五 19 英语 60
<script> window.onload=function(){tablecols("tab",4); }</script>

表格代码