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. 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:
The 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:
The Code is as follows:
Document. write (" ");
The two types of htmlElement method. The first method is more standard, the code is more, and the second method is less code.