Use of the Document.createelement method

Source: Internet
Author: User

When we use the Createelemen method t to create an element node, it seems to be able to write under IE, but switching to FF and other browsers will always error.

For example, if we want to create an INPUT element, then under IE, we can have a variety of ways:

To test a situation, note the other situation:

<script type= "Text/javascript" >
The first, along with attributes, is created
var x = document.createelement ("<input type= ' text ' size= '/>");
Second, create in an empty label form
var x = document.createelement ("<input/>");
Third, created in the form of tagname
var x = document.createelement ("input");

Document.body.appendChild (x);
</script>

The above 3 kinds of ways in IE can pass, and then non-IE core browser (such as FF,OP,SF, etc.), the first 2 kinds of writing will be error, can not create this input element correctly.

The standard is written as a 3rd, and the browser is accurate.

Because the attributes of the element can be created together when creating an element node in IE using the CreateElement method, this is not possible in other browsers, so we have to create its properties in other ways.

To create a standard notation for an element node with attributes:

<script type= "Text/javascript" >
var x = document.createelement ("input");
X.setattribute ("type", "text");
X.setattribute ("Size", "30");

Document.body.appendChild (x);
</script>

Add a property on Document.setattribute ("attribute", "value") once.

Later found that actually can also write:

<script type= "Text/javascript" >
var x = document.createelement ("input");
X.type = "text";

Document.body.appendChild (x);
</script>

But using this to create properties, it seems that not all attributes can be created, such as input size,value,class and so on in this way can not be created, for specific reasons I am not clear. So it is recommended to use the previous wording.

Use of the Document.createelement method

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.