Create a table dynamically using javascript, and create a table using javascript
Copy codeThe Code is as follows:
<Html>
<Head>
<Script>
Function createTable (rows, lines ){
This. rows = rows;
This. lines = lines;
Var Body = document. getElementById ('body ');
Var Table = document. createElement ('table'); // create a table label element
Table. setAttribute ('border', '1 ');
// Add other attributes to the table label
For (var I = 0; I <this. rows; I ++ ){
Var lRow = document. createElement ('tr ');
For (var j = 0; j <this. lines; j ++ ){
Var textNode = document. createTextNode (I + ',' + j );
Var lLine = document. createElement ('td ');
LLine. appendChild (textNode );
LRow. appendChild (lLine );
}
Table. appendChild (lRow );
}
Body. appendChild (Table );
}
</Script>
</Head>
<Body>
<Div id = "body"> </div>
</Body>
<Script type = "text/javascript">
CreateTable (10, 10 );
</Script>
</Html>
Method 2:
Copy codeThe Code is as follows:
<Script>
Function createTable (rows, lines ){
This. rows = rows;
This. lines = lines;
Var Body = document. getElementById ('body ');
Var Table = document. createElement ('table ');
Table. setAttribute ('border', 1 );
For (var I = 0; I <this. rows; I ++ ){
Var row = Table. insertRow (I );
For (var j = 0; j <this. lines; j ++ ){
Var cells = row. insertCell (j );
Cells. innerHTML = I + ',' + j
}
}
Body. appendChild (Table );
}
</Script>