代码: 复制代码 代码如下: merge.html <BR>function merge1(){ //可实现单元格,通过给 开始cell的比较 <BR>var totalRow = $("#tbl").find("tr").length; <BR>var totalCol = $("#tbl").find("tr").eq(0).find("td").length; <BR>for(var col=totalCol-1;col>=1;col--){ <BR>spanNum =1; <BR>startCell = $("#tbl").find("tr").eq(totalRow-1).find("td").eq(col); <BR>for(var row = totalRow-1;row>=1;row--){ <BR>targetCell = $("#tbl").find("tr").eq(row-1).find("td").eq(col); <BR>if(startCell.text() == targetCell.text() && startCell.text()!=""){ <BR>spanNum++; <BR>targetCell.attr("rowSpan",spanNum); <BR>startCell.remove(); <BR>}else{ <BR>spanNum =1; <BR>} <BR>startCell = targetCell; <BR>} <BR>} <BR>} <BR>function merge2() { //可实现合并单元格,上下行来比较 <BR>var totalCols = $("#tbl").find("tr:eq(0)").find("td").length; <BR>var totalRows = $("#tbl").find("tr").length; <BR>for ( var i = totalCols-1; i >= 1; i--) { <BR>for ( var j = totalRows-1; j >= 1; j--) { <BR>startCell = $("#tbl").find("tr").eq(j).find("td").eq(i); <BR>targetCell = $("#tbl").find("tr").eq(j - 1).find("td").eq(i); <BR>if (startCell.text() == targetCell.text() && targetCell.text() != "") { <BR>targetCell.attr("rowSpan", (startCell.attr("rowSpan")==undefined)?2:(eval(startCell.attr("rowSpan"))+1)); <BR>startCell.remove(); <BR>} <BR>} <BR>} <BR>} <BR>/*先合并,使用style 的display:none将相同元素隐藏,然后再remove <BR>*/ <BR>function merge3(){ <BR>var totalCols = $("#tbl").find("tr:eq(0)").find("td").length; <BR>var totalRows = $("#tbl").find("tr").length; <BR>for(var col=totalCols-1;col>=1;col--){ <BR>spanNum =1; <BR>startCell = $("#tbl").find("tr").eq(totalRows-1).find("td").eq(col); <BR>for(var row = totalRows-1;row>=1;row--){ <BR>targetCell = $("#tbl").find("tr").eq(row-1).find("td").eq(col); <BR>if(startCell.text() == targetCell.text() && startCell.text()!=""){ <BR>spanNum++; <BR>targetCell.attr("rowSpan",spanNum); <BR>startCell.attr("style","visibility:hidden"); <BR>// startCell.attr("style","display:none"); <BR>}else{ <BR>spanNum =1; <BR>} <BR>startCell = targetCell; <BR>} <BR>} <BR>for(var j=totalCols-1;j>=1;j--){ <BR>for(var i=totalRows-1;i>=1;i--){ <BR>cell = $("#tbl").find("tr").eq(i).find("td").eq(j); <BR>if(cell.attr("style")!=undefined){ <BR>if(cell.attr("style")=="visibility:hidden"){ <BR>cell.remove(); <BR>} <BR>} <BR>} <BR>} <BR>} <BR>function merge4(){ //与merge3方法类似,目的是看一下 display:none与visibility:hidden的效果区别 <BR>var totalCols = $("#tbl").find("tr:eq(0)").find("td").length; <BR>var totalRows = $("#tbl").find("tr").length; <BR>for(var col=totalCols-1;col>=1;col--){ <BR>spanNum =1; <BR>startCell = $("#tbl").find("tr").eq(totalRows-1).find("td").eq(col); <BR>for(var row = totalRows-1;row>=1;row--){ <BR>targetCell = $("#tbl").find("tr").eq(row-1).find("td").eq(col); <BR>if(startCell.text() == targetCell.text() && startCell.text()!=""){ <BR>spanNum++; <BR>targetCell.attr("rowSpan",spanNum); <BR>startCell.attr("style","display:none"); <BR>// startCell.attr("style","display:none"); <BR>}else{ <BR>spanNum =1; <BR>} <BR>startCell = targetCell; <BR>} <BR>} <BR>for(var j=totalCols-1;j>=1;j--){ <BR>for(var i=totalRows-1;i>=1;i--){ <BR>cell = $("#tbl").find("tr").eq(i).find("td").eq(j); <BR>if(cell.attr("style")!=undefined){ <BR>if(cell.attr("style")=="display:none"){ <BR>cell.remove(); <BR>} <BR>} <BR>} <BR>} <BR>} <BR> 销售时间 裙子 裤子 风衣 鞋子 8:00-9:00 3 4 9:00-10:00 3 2 5 3 10:00-11:00 3 2 1 11:00-12:00 1 总结:在使用remove的时候,一定要注意如果 从list的开始元素循环下去,remove掉一个元素后,有些元素就找不到了或者说不是要找的那个元素;最好是从后面开始循环。