jquery还原含有rowspan、colspan的table

简介:

需求

  把含有rowspan、colspan的table还原。

  例如原table为:

  还原后的table为:

 

代码原理

  对table进行遍历,如果td的rowspan属性值大于1,则给当前的td的父元素的兄弟元素添加td,如果td的colspan属性值大于1,则在当前的td元素后添加td

完整代码

//本文首发博客园:http://artwl.cnblogs.com(2012/02/08)
jQuery.fn.RevertTable=function(){
$("tr",this).each(function(trindex,tritem){
$(tritem).find("td").each(function(tdindex,tditem){
var rowspanCount=$(tditem).attr("rowspan");
var colspanCount=$(tditem).attr("colspan");
var value=$(tditem).text();
var newtd="<td>"+value+"</td>";
if(rowspanCount>1){
var parent=$(tditem).parent("tr")[0];
while(rowspanCount-->1){
$($(parent).next()[0].children[tdindex]).before(newtd);
parent=$(parent).next();
}
$(tditem).attr("rowspan",1);
}
if(colspanCount>1){
while(colspanCount-->1){
$(tditem).after(newtd);
}
$(tditem).attr("colspan",1);
}
});
});
}

测试案例

小结

  本文只提供了还原含有rowspan、colspan的table的方案之一,欢迎大家测试讨论。

  至于合并表格单元格网上已经有了代码:

  原文标题:jQuery colspan and rowspan table using cell break

  原文地址:http://willifirulais.blogspot.com/2007/07/jquery-table-column-break.html

  本文首发博客园:http://artwl.cnblogs.com




本文转自Artwl博客园博客,原文链接:http://www.cnblogs.com/artwl/,如需转载请自行联系原作者

相关文章
|
JavaScript
jquery 实现 table 和 标题 的联动显示
jquery 实现 table 和 标题 的联动显示
123 0
jquery 实现 table 和 标题 的联动显示
|
JavaScript
jquery获取table,遍历输出tr中各个td的内容(转载)
首先,依赖jquery.. 1 $('#btntb').click(function(){ 2 $('#tab tr').each(function(i){ // 遍历 tr 3 $(this).children('td').each(function(j){ // 遍历 tr 的各个 td 4 alert("第"+(i+1)+"行,第"+(j+1)+"个td的值:"+$(this).text()+"。
3206 0
|
JavaScript
jQuery下table操作示例(附案例源码)
jQuery下table操作示例(附案例源码)
124 0
jQuery下table操作示例(附案例源码)
|
JavaScript
用jquery删除table列表中&lt;u&gt;标签
//循环去掉a和u标签$("tbody a").each(function(){          var xx=$(this).find("u").
1014 0
|
前端开发 JavaScript
JQuery焦点Table
.prod_description_sizechart table{margin-bottom:0;width:992px;max-width:none;border-collapse:collapse;background-color:transparent;table-layout:fixed;border-spacing:0;} .
557 0