Question about dynamic DOM element creation based on jquery

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Document. getElementById ("testDiv"). innerHTML = "dynamically created div ";
</Script>

I am still happy to use it, but how many people know it is wrong! Cause of error:
(1) The structure of the page is changed during page loading. in IE6, if the network slows down or the page content is too large, the "stop operation" error will occur. that is to say, "Never change the Dom model of a page during page loading ".
(2) Adding Elements Using Modified HTML content does not comply with Dom standards. in actual work, after using this method to modify the content, Some browsers cannot immediately display the added elements, because the display engines of different browsers are different. however, if we use the CreateElement of Dom to create an object, almost all browsers can. however, if jQuery is passed in as a complete HTML string, innerHTML is also used internally. therefore, the use of the innerHTML function is not completely negative. so from now on, Please discard this old knowledge and use the correct programming method described below.
This article does not detail how to create elements using html dom. The following is a simple example: the first correct method:
Copy codeThe Code is as follows:
// Use the Dom standard to create elements
Var select = document. createElement ("select ");
Select. options [0] = new Option ("add-on 1", "value1 ");
Select. options [1] = new Option ("add-on 2", "value2 ");
Select. size = "2 ";
Var object = testDiv. appendChild (select );

By using the document. createElement method, we can create Dom elements and add them to the specified object through the appendChild method.
Method 2: Use Jquery
When the HTML string is an element without attributes, the document. createElement is used internally to create elements, for example:

// Use document. createElement to create an element in jQuery:


$ ("Maid (" border "," solid 1px # FF0000 "Fig (" dynamically created div "). appendTo (testDiv );
Otherwise, use the innerHTML method to create elements:

// Use innerHTML to create an element in jQuery:

$ ("Dynamically created div"). appendTo (testDiv)

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.