Merge JavaScript cells and JavaScript Cells

Source: Internet
Author: User

 

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

<Title> merge tables </title>

<Script>

//////////////////////////////////////// ///////

// Function: Merge tables

// Parameter: tb-ID of the table to be merged

// Parameter: colLength -- the first few columns need to be merged, for example,

// If you want to merge the first two columns, ignore the merging of the following data columns. colLength should be 2

// Merge all columns by default

// Data: 2011.11.06

//////////////////////////////////////// ///////

Function uniteTable (tb, colLength ){

// Check whether the table is regular

If (! CheckTable (tb) return;

Var I = 0;

Var j = 0;

Var rowCount = tb. rows. length; // number of rows

Var colCount = tb. rows [0]. cells. length; // Number of Columns

Var obj1 = null;

Var obj2 = null;

// Name each cell

For (I = 0; I <rowCount; I ++ ){

For (j = 0; j <colCount; j ++ ){

Tb. rows [I]. cells [j]. id = "tb _" + I. toString () + "_" + j. toString ();

}

}

// Check and merge by Column

For (I = 0; I <colCount; I ++ ){

If (I = colLength) return;

Obj1 = document. getElementById ("tb _ 0 _" + I. toString ())

For (j = 1; j <rowCount; j ++ ){

Obj2 = document. getElementById ("tb _" + j. toString () + "_" + I. toString ());

If (obj1.innerText = obj2.innerText ){

Obj1.rowSpan ++;

Obj2.parentNode. removeChild (obj2 );

} Else {

Obj1 = document. getElementById ("tb _" + j. toString () + "_" + I. toString ());

}

}

}

}

/////////////////////////////////////////

// Function: Check whether the table is regular

// Parameter: tb -- ID of the table to be checked

// Data: 2011.11.06

/////////////////////////////////////////

Function checkTable (tb ){

If (tb. rows. length = 0) return false;

If (tb. rows [0]. cells. length = 0) return false;

For (var I = 0; I <tb. rows. length; I ++ ){

If (tb. rows [0]. cells. length! = Tb. rows [I]. cells. length) return false;

}

Return true;

}

</Script>

</Head>

<Body>

<Table width = "400" border = "1" id = "table1">

<Tr>

<Td> a </td>

<Td> for </td>

<Td> 100 </td>

<Td> 200 </td>

<Td> 1 </td>

</Tr>

<Tr>

<Td> a </td>

<Td> for </td>

<Td> 100 </td>

<Td> 300 </td>

<Td> 2 </td>

</Tr>

<Tr>

<Td> a </td> www.2cto.com

<Td> if </td>

<Td> 100 </td>

<Td> 200 </td>

<Td> 3 </td>

</Tr>

<Tr>

<Td> a </td>

<Td> if </td>

<Td> 300 </td>

<Td> 230 </td>

<Td> 4 </td>

</Tr>

<Tr>

<Td> a </td>

<Td> if </td>

<Td> 320 </td>

<Td> 230 </td>

<Td> 5 </td>

</Tr>

</Table>

<Br> <input type = "button" value = "MERGE table" onClick = "uniteTable (table1, 4)">

</Body>

</Html>,

 

From crawling forward

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.