This function can be used directly to merge the situations where the upper and lower cells have the same value. For example, I have written comments. What you can understand is probably different from what you want, but you can make modifications on this basis. FunctionrowSpan () {// obtain the table object varta... SyntaxHighlighter. all (). This function can be used directly to merge the rows with the same upper and lower cell values. Effect:
I have written comments. You can understand the comments, but they may not meet your requirements. But you can modify the comments on this basis.
Function rowSpan (){
// Obtain the table object
Var tab = document. getElementById ("tab ");
If (! Tab ){
Return;
}
// Exclude the title line from the second line
Var startRow = 1;
// Record the content of td in the table cyclically to determine whether the value in td has changed
Var tdTempV = "";
// If the td value is the same, add 1 to the variable; otherwise, add the temporary variable to the set.
Var rowCount = 1;
// Obtain the set www.2cto.com with the same number of rows
Var totalcount = new Array ();
For (var I = 1; I // Obtain the td value first.
Var tdText = tab. rows [I]. cells [0]. innerText;
// If this is the first cycle, directly continue;
If (I = startRow ){
TdTempV = tdText;
Continue;
}
// If the current value is the same as the source value, add 1 to the temporary quantity; otherwise, add it to the set.
If (tdTempV = tdText ){
RowCount ++;
} Else {
Totalcount. push (rowCount );
TdTempV = tdText;
RowCount = 1;
}
// Determine whether it is the last cycle. If it is the last one, the current quantity is directly stored in the set.
If (I = tab. rows. length-1 ){
Totalcount. push (rowCount );
}
}
// Temporary variable, judge whether it is the same as a value in the array during recycling
Var tNum = 0;
// Note that this cycle is reversed.
For (var I = tab. rows. length-1; I >= startRow; I --){
// Temporary variables, store td
Var tTd = tab. rows [I]. cells [0];
TNum ++;
// If the tNum value is the same as the last value in the array, it can be determined that the same td has ended [only one of them]
If (tNum = totalcount [totalcount. length-1]) {
// Add the rowSpan attribute to the current td
TTd. setAttribute ("rowSpan", totalcount [totalcount. length-1]);
// Bring up the last element of the array
Totalcount. pop ();
TNum = 0;
} Else {
// Delete the current td
Tab. rows [I]. removeChild (tTd );
}
}
}
From zhangyuhong's column