熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JQuery實現表格中相同單元格合並示例代碼

2022-06-13   來源: JSP教程 
一定要注意如果從list的開始元素循環下去remove掉一個元素後有些元素就找不到了或者說不是要找的那個元素感興趣的各位可以研究下哈  

  代碼

復制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<title>mergehtml</title>
<meta httpequiv="keywords" content="keywordkeywordkeyword">
<meta httpequiv="description" content="this is my page">
<meta httpequiv="contenttype" content="text/html; charset=UTF">
<link rel="stylesheet" href="css/jqueryuicss" />
<script src="js/jqueryjs"></script>
<script src="js/jqueryuijs"></script>
<script type="text/javascript">
function merge(){ //可實現單元格通過給 開始cell的比較
var totalRow = $("#tbl")find("tr")length;
var totalCol = $("#tbl")find("tr")eq()find("td")length;
for(var col=totalCol;col>=;col){
spanNum =;
startCell = $("#tbl")find("tr")eq(totalRow)find("td")eq(col);
for(var row = totalRow;row>=;row){
targetCell = $("#tbl")find("tr")eq(row)find("td")eq(col);
if(startCelltext() == targetCelltext() && startCelltext()!=""){
spanNum++;
targetCellattr("rowSpan"spanNum);
startCellremove();
}else{
spanNum =;
}
startCell = targetCell;
}
}
}
function merge() { //可實現合並單元格上下行來比較
var totalCols = $("#tbl")find("tr:eq()")find("td")length;
var totalRows = $("#tbl")find("tr")length;
for ( var i = totalCols; i >= ; i) {
for ( var j = totalRows; j >= ; j) {
startCell = $("#tbl")find("tr")eq(j)find("td")eq(i);
targetCell = $("#tbl")find("tr")eq(j )find("td")eq(i);
if (startCelltext() == targetCelltext() && targetCelltext() != "") {
targetCellattr("rowSpan" (startCellattr("rowSpan")==undefined)?:(eval(startCellattr("rowSpan"))+));
startCellremove();
}
}
}
}
/*先合並使用style 的display:none將相同元素隱藏然後再remove
*/
function merge(){
var totalCols = $("#tbl")find("tr:eq()")find("td")length;
var totalRows = $("#tbl")find("tr")length;
for(var col=totalCols;col>=;col){
spanNum =;
startCell = $("#tbl")find("tr")eq(totalRows)find("td")eq(col);
for(var row = totalRows;row>=;row){
targetCell = $("#tbl")find("tr")eq(row)find("td")eq(col);
if(startCelltext() == targetCelltext() && startCelltext()!=""){
spanNum++;
targetCellattr("rowSpan"spanNum);
startCellattr("style""visibility:hidden");
// startCellattr("style""display:none");
}else{
spanNum =;
}
startCell = targetCell;
}
}
for(var j=totalCols;j>=;j){
for(var i=totalRows;i>=;i){
cell = $("#tbl")find("tr")eq(i)find("td")eq(j);
if(cellattr("style")!=undefined){
if(cellattr("style")=="visibility:hidden"){
cellremove();
}
}
}
}
}
function merge(){ //與merge方法類似目的是看一下 display:none與visibility:hidden的效果區別
var totalCols = $("#tbl")find("tr:eq()")find("td")length;
var totalRows = $("#tbl")find("tr")length;
for(var col=totalCols;col>=;col){
spanNum =;
startCell = $("#tbl")find("tr")eq(totalRows)find("td")eq(col);
for(var row = totalRows;row>=;row){
targetCell = $("#tbl")find("tr")eq(row)find("td")eq(col);
if(startCelltext() == targetCelltext() && startCelltext()!=""){
spanNum++;
targetCellattr("rowSpan"spanNum);
startCellattr("style""display:none");
// startCellattr("style""display:none");
}else{
spanNum =;
}
startCell = targetCell;
}
}
for(var j=totalCols;j>=;j){
for(var i=totalRows;i>=;i){
cell = $("#tbl")find("tr")eq(i)find("td")eq(j);
if(cellattr("style")!=undefined){
if(cellattr("style")=="display:none"){
cellremove();
}
}
}
}
}
</script>
</head>
<body>
<table id="tbl" cellpadding="" border=>
<thead>
<tr>
<td>銷售時間</td>
<td>裙子</td>
<td>褲子</td>
<td>風衣</td>
<td>鞋子</td>
</tr>
</thead>
<tbody>
<tr>
<td>::</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>::</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>::</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>::</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<input type="button" value="合並" id="merge" onclick="merge();">
</body>
</html>

  
總結在使用remove的時候一定要注意如果 從list的開始元素循環下去remove掉一個元素後有些元素就找不到了或者說不是要找的那個元素最好是從後面開始循環


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19873.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.