Document. createelement () Usage

Source: Internet
Author: User

AnalysisCodeFind your own blind spot-document. createelement (), surf and summarize some experience.

 

Document. createelement () is an object created in the object. It must be used together with the appendchild () or insertbefore () method. The appendchild () method adds a new subnode at the end of the subnode list of the node. The insertbefore () method inserts a new node anywhere in the node's subnode list.

 

The following is an exampleDocument. createelement () Usage. <Div id = "board"> </div>

Example 1:

<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 tab board with the property value "this is a small example of test loading ".

Example 2:

<SCRIPT type = "text/JavaScript">
VaR board = Document. getelementbyid ("Board ");
VaR e2 = Document. createelement ("select ");
E2.options [0] = New Option ("add-on 1 ","");
E2.options [1] = New Option ("add-on 2 ","");
E2.size = "2 ";
VaR object = board. appendchild (E2 );
</SCRIPT>

Effect:Load a drop-down list box on the tab board. The attribute values are "add-on 1" and "add-on 2 ".

 

Example 3:

<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 ('this is a test! ');");
VaR object = board. appendchild (E3 );
</SCRIPT>

Effect:Load a text box in the tab board with the property value "use setattribute ". When you click this text box, the "this is a test!" dialog box is displayed !".

 

According to the above example, we can see that the parameters are the same by loading the object attributes. Use E. type = "text" and E. setattribute ("type", "text") to achieve the same effect.

Next, we will use an example to describe the differences between the appendchild () method and the insertbefore () method..

For example, when we want to insert a subnode P in the following Div: <Div id = "test"> <p id = "X1"> node </P> <p> node </P> </div>

We can write 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 from here.
// Appendchild method:
Otest. appendchild (newnode );
// Insertbefore method:
Otest. insertbefore (newnode, null );
</SCRIPT>

Through the above code, we can test that a new node is created under the node Div, And the node is a divLast Node. Obviously, through this example, we can know that both appendchildhild and insertbefore can be used to insert nodes.

In the above example, there is a code: otest. insertbefore (newnode, null). Here, insertbefore has two parameters that can be set. The first parameter is the same as appendchild, but the second parameter is unique to it.It can be either null or:

<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:In this example, a new node is inserted before the X1 node.

 

Or:

<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:In this example, a new node is inserted before the next node of the X1 node.

 

It can also be:

<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>

In this example, a new node is inserted before the first subnode. You can also insert a new node in another location by changing childnodes [,...].

As you can see, the insertbefore () method inserts a new node before an existing subnode.But in Example 1, The insertbefore () method can also be used to insert a new node at the end of the subnode list. The two conditions are combined to find that the insertbefore () method can insert nodes anywhere in the subnode list.

The following examples show that:

Appendchild () method adds a new subnode at the end of the subnode list of the node.

The insertbefore () method inserts a new node anywhere in the node's subnode list.

 

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.