Requirement
Restore the table containing rowspan and colspan.
For example, the original table is:
The restored table is:
Code Principle
Traverse the table. If the rowspan attribute value of td is greater than 1, add td to the sibling element of the parent element of the current td. If the colspan attribute value of td is greater than 1 , then add td
Copy code after the current td element. The code is as follows:
// The first blog of this article: 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="
" value " | ";
if(rowspanCount>1){
var parent=$(tditem).parent("tr")[0];
while(rowspanCount-->1){
$(parent). next().prepend(newtd);
parent=$(parent).next();
}
$(tditem).attr("rowspan",1);
}
if(colspanCount>1){
while(colspanCount-->1){
$(tditem).after(newtd);
}
$(tditem).attr("colspan ",1);
}
});
});
}
Online demo
http://demo.jb51.net/js /2012/jquery_demo/jquery_rowspan_colspan_table.html
Summary
This article only provides one of the solutions to restore the table containing rowspan and colspan. Everyone is welcome to test and discuss.
As for merging table cells, the code is already available online:
Original title: jQuery colspan and rowspan table using cell break
Original address: http://willifirulais.blogspot.com/2007/07/jquery- table-column-break.html