The method for generating HTML elements in Javascript is described in this article. The first method is a more formal method for creating elements, and the second method is to use the write () method in javascript to directly write html elements.
Method 1:
Copy codeThe Code is as follows:
// CreateElement () creates the input element to the obj object.
Var obj = document. createElement ('input ');
// Select the previous element of the location to be generated
Var before = document. getElementById ('before ');
// Set the obj element name and Value
Obj. name = 'name ';
Obj. value = 'value ';
// SetAttribute can define attributes freely, not only id and type, name and value, but also
Obj. setAttribute ('id', 'idname ');
Obj. setAttribute ('type', 'typename ');
// Add obj after the previous Element
Before. appendChild (obj );
Method 2:
Copy codeThe Code is as follows:
Document. write ("<inpt name = 'name' value = 'value' id = 'idname' type = 'typename'> ");
Here we list the two methods for generating html <input> elements. The first method is more standard, the code is more, and the second method is less code.