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]