Home >Web Front-end >JS Tutorial >Implementation method of restoring table containing rowspan and colspan through jquery_jquery

Implementation method of restoring table containing rowspan and colspan through jquery_jquery

WBOY
WBOYOriginal
2016-05-16 17:56:231396browse

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 demohttp://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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn