Javascript script code for dynamically adding cells

Source: Internet
Author: User

How to dynamically add cells !!
Copy codeThe Code is as follows:
Use insertRow () and insertCell () Methods

The usage is as follows:
InsertRow ()
Copy codeThe Code is as follows:
Var newTR = TableOBJ. insertRow (N );

Copy codeThe Code is as follows:
N indicates the row to which the data is inserted!
Note: When N = 0, insert it to the top row! This parameter is omitted when N =-1 is inserted to the last line!
InsertCell ()

 Copy codeThe Code is as follows:
Var newTD = TrOBJ. insertCell (N );

N indicates the column to which the data is inserted!
Note: When N = 0, insert it to the top row! This parameter is omitted when N =-1 is inserted to the last line!
The following is an example code:
Copy codeThe Code is as follows:

<Script language = "JavaScript">
Var Count = false, NO = 1;
Function addRow (){
Count =! Count;
// Add a row
Var newTr = testTbl. insertRow ();
// Add three columns
Var newTd0 = newTr. insertCell ();
Var newTd1 = newTr. insertCell ();
Var newTd2 = newTr. insertCell ();
// Set the column content and attributes
If (Count) {newTr. style. background = "# FFE1FF ";}
Else {newTr. style. background = "# FFEFD5 ";}
NewTd0.innerHTML = '<input type = checkbox id = "box4"> ';
NO ++
NewTd1.innerText = "row" + NO + ";
}
</Script>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> Blu-Ray-BlueShine </title> </pead> <body> <table width =" 399 "border = 0 cellspacing =" 1 "id = "testTbl" style = "font-size: 14px; "> <tr bgcolor =" # FFEFD5 "> <td width = 6%> <input type = checkbox id =" box1 "> </td> <td> 1st rows </td> <td> </tr> </table> <label> <input type = "button" value = "insert row" onclick = "addRow () "/> </label> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.