Example of merging the same content of table td by js _ javascript tips-js tutorial

Source: Internet
Author: User
As the title says, how to merge the same content of the table's td is described in detail. The following describes how to use js. Do not miss out if you are interested. The Code is as follows:


Function hb (){
Var tab = document. getElementById ("subtable ");
Var maxCol = 3, val, count, start;
Var ys = "";
For (var col = maxCol-1; col> = 0; col --){
Count = 1;
Val = "";
For (var I = 0; I If (val = tab. rows [I]. cells [col]. innerHTML ){
Count ++;
} Else {
If (count> 1 ){
// Merge
Start = I-count;
If (ys = "#00 FFFF "){
Ys = "# EEEE00 ";
} Else {
Ys = "#00 FFFF ";
}
Tab. rows [start]. cells [col]. rowSpan = count;
Tab. rows [start]. cells [1]. style. backgroundColor = ys; // change the color.
// Ys = "# EEEE00 ";
// Tab. rows [I]. cells [1]. style. backgroundColor = "#00 FFFF"; // change the color to green.
For (var j = start + 1; j Tab. rows [j]. cells [col]. style. display = "none ";
Tab. rows [j]. removeChild (tab. rows [j]. cells [col]);
}
Count = 1;
}
Val = tab. rows [I]. cells [col]. innerHTML;
}
}

If (count> 1) {// merge, when the last few rows are the same
Start = I-count;
Tab. rows [start]. cells [col]. rowSpan = count;
For (var j = start + 1; j Tab. rows [j]. removeChild (tab. rows [j]. cells [col]);
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.