The use of--document.createelement () of JavaScript techniques

Source: Internet
Author: User
Document_createelement_x_x () uses the analysis code when discovering its own blind spot--document_createelement_x_x (), surfing a bit, summarizing the point of experience.

Document_createelement_x_x () is an object that is created in an object and is used in conjunction with a () or InsertBefore () method. where the A () 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_x_x () . <div id= "board" ></div>

Example 1:

<script type= "Text/javascript" >
var board = document.getelementbyidx_x_x ("board");
var e = document_createelement_x_x("input");
e.type = "button";
E.value = "This is a small example of test loading";
var object = board. a (e);
</script>

effect: load a button in the label board, the property value is "This is a small example of test loading."

Example 2:

<script type= "Text/javascript" >
var board = document.getelementbyidx_x_x ("board");
var e2 = document_createelement_x_x ("select");
E2.options[0] = new Option ("Add-in 1", "");
E2.OPTIONS[1] = new Option ("Add-in 2", "");
E2.size = "2";
var object = BOARD.A (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:

<script type= "Text/javascript" >
var board = document.getelementbyidx_x_x ("board");
var E3 = document_createelement_x_x ("input");
e4.setattribute ("type", "text");
E4.setattribute ("name", "Q");
E4.setattribute ("value", "Use setattribute");
E4.setattribute ("onclick", "Javascript:alert (' a test! ');");
var object = BOARD.A (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 A () 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:

<script type= "Text/javascript" >
var otest = document.getelementbyidx_x_x ("test");
var newNode = document_createelement_x_x ("P");
newnode.innerhtml = "This is a test";
The test starts here.
A method:
OTEST.A (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 Ahild 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 a, and the second is unique. not only can it be null, but it can also be:

<script type= "Text/javascript" >
var otest = document.getelementbyidx_x_x ("test");
var refchild = document.getelementbyidx_x_x ("x1");
var newNode = document_createelement_x_x ("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:

<script type= "Text/javascript" >
var otest = document.getelementbyidx_x_x ("test");
var refchild = document.getelementbyidx_x_x ("x1");
var newNode = document_createelement_x_x ("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:

<script type= "Text/javascript" >
var otest = document.getelementbyidx_x_x ("test");
var newNode = document_createelement_x_x ("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 A () 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 InsertBefore () method inserts a new node at any point in the node's child node list.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.