Document.createelement () is an object that is created in an object and is used in conjunction with the appendchild () or InsertBefore () method. where the AppendChild () method adds a new child node at the end of the node's child node list. The InsertBefore () method inserts a new node at any point in the node's child node list.
The following example illustrates the use of document.createelement (). <div id= "board" ></div>
Example 1:
Copy Code code as follows:
<script type= "Text/javascript" >
var board = document.getElementById ("board");
var e = document.createelement ("input");
E.type = "button";
E.value = "This is a small example of test loading";
var object = Board.appendchild (e);
</script>
Effect: Load a button in the label board, the property value is "This is a small example of test loading."
Example 2:
Copy Code code as follows:
<script type= "Text/javascript" >
var board = document.getElementById ("board");
var e2 = document.createelement ("select");
E2.options[0] = new Option ("Add-in 1", "");
E2.OPTIONS[1] = new Option ("Add-in 2", "");
E2.size = "2";
var object = Board.appendchild (E2);
</script>
Effects: Loads a drop-down list box in the label board, with the property value "Add-in 1" and "Add-in 2".
Example 3:
Copy Code code as follows:
<script type= "Text/javascript" >
var board = document.getElementById ("board");
var E3 = document.createelement ("input");
E4.setattribute ("type", "text");
E4.setattribute ("name", "Q");
E4.setattribute ("value", "Use setattribute");
E4.setattribute ("onclick", "Javascript:alert (' a test! ');");
var object = Board.appendchild (E3);
</script>
Effects: Loads a text box in the label board, and the property value is "Use SetAttribute". When you click this text box, the dialog box "This is a test!" will pop up.
As you can see from the example above, you can set the parameters by loading the properties of the object. Using e.type= "text" and E.setattribute ("type", "text") effect is consistent.
Below, we use an example to tell the difference between the AppendChild () method and the InsertBefore () method.
For example, we want to insert a child node p in the following div: <div id= "test" ><p id= "x1" >Node</p><p>Node</p></div>
We can write this:
Copy Code code as follows:
<script type= "Text/javascript" >
var otest = document.getElementById ("test");
var newNode = document.createelement ("P");
newnode.innerhtml = "This is a test";
The test starts here.
AppendChild Method:
Otest.appendchild (NewNode);
InsertBefore Method:
Otest.insertbefore (Newnode,null);
</script>
Through the above code, you can test that a new node is created under the node Div, and that the node is the last div node. Obviously, with this example, you know that Appendchildhild and insertbefore can be inserted into the node operation.
In the above example there is a code: Otest.insertbefore (Newnode,null), where InsertBefore has 2 parameters can be set, the first is the same as AppendChild, and the second is unique. Not only can it be null, but it can also be:
Copy Code code as follows:
<script type= "Text/javascript" >
var otest = document.getElementById ("test");
var refchild = document.getElementById ("x1");
var newNode = document.createelement ("P");
newnode.innerhtml = "This is a test";
Otest.insertbefore (Newnode,refchild);
</script>
Effect: This example inserts a new node in front of the X1 node
Also or:
Copy Code code as follows:
<script type= "Text/javascript" >
var otest = document.getElementById ("test");
var refchild = document.getElementById ("x1");
var newNode = document.createelement ("P");
newnode.innerhtml = "This is a test";
Otest.insertbefore (newnode,refchild.nextsibling);
</script>
Effect: This example inserts a new node in front of the next node in the X1 node
can also be:
Copy Code code as follows:
<script type= "Text/javascript" >
var otest = document.getElementById ("test");
var newNode = document.createelement ("P");
newnode.innerhtml = "This is a test";
Otest.insertbefore (Newnode,otest.childnodes[0]);
</script>
This example inserts a new node in front of the first child node, or by changing the childnodes[0,1,...] To insert a new node in another location
Because the attribute of the Visible insertbefore () method is to insert a new node in front of an existing child node, the InsertBefore () method can also be used to insert the new node at the end of the list of child nodes. In combination, it is found that the InsertBefore () method inserts a node, which can be anywhere in the child node list.
From the following examples:
The AppendChild () method adds a new child node at the end of the node's child node list.
The InsertBefore () method inserts a new node at any point in the node's child node list.