javascript

位置:IT落伍者 >> javascript >> 浏览文章

JQuery实现表格中相同单元格合并示例代码


发布日期:2021年09月17日
 
JQuery实现表格中相同单元格合并示例代码
一定要注意如果从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掉一个元素后有些元素就找不到了或者说不是要找的那个元素最好是从后面开始循环

上一篇:Js日期选择器并自动加入到输入框中示例代码

下一篇:js innerHTML 改变div内容的方法