CodePudding user response:
Please try from the following HTML example, it USES the method of DOM 1 by JavaScript to dynamically create an HTML form, it creates a consisting of four have text content cell of small tables, the cell's written content is: "the cell is the first line of the first column" x y, said in the number of rows and columns in a table cell,<script>
The function start () {
//get the body tag
Var mybody=document. GetElementsByTagName (" body ") [0];
//create a & lt; Table> Elements and a & lt; Tbody> The element
Mytable=document. The createElement method (" table ");
Mytablebody=document. The createElement method (" tbody ");
//create all cell
For (var j=0; J & lt; 2; J++) {
//create a & lt; tr> The element
Mycurrent_row=document. The createElement method (" tr ");
for(var i=0; i <2; I++) {
//create a & lt; td> The element
Mycurrent_cell=document. The createElement method (" td ");
//create a text node
Currenttext=document. CreateTextNode (" cell is the first "+ j +", the first "+ I +" column ");
//creates a text node is added to the & lt; td> The
Mycurrent_cell. The appendChild (currenttext);
//the column & lt; td> Added to the line & lt; tr>
Mycurrent_row. The appendChild (mycurrent_cell);
}
//will line & lt; tr> Added to the & lt; Tbody>
Mytablebody. The appendChild (mycurrent_row);
}
//will & lt; Tbody> Added to the & lt; Table>
Mytable. The appendChild (mytablebody);
//will & lt; Table> Added to the & lt; body>
Mybody. The appendChild (mytable);
//will table mytable border attribute is set to 2
Mytable. SetAttribute (" border ", "2");
}
</script>