JavaScript dynamically create div attention to detail

Source: Internet
Author: User

(1) Changes the structure of the page when the page is loaded. A "Terminate operation" error occurs in IE6 if the network slows down or the page content is too large. This means "never change the DOM model of a page when the page is loaded."

(2) Add elements using modified HTML content and do not conform to the DOM standard. In the actual work has also encountered the use of this method to modify the content,
Added elements are not immediately visible in some browsers because the display engine for different browsers is different. But if we use Dom's createelement to create objects,
Almost all of them are available in all browsers. But in jquery, if you're passing in a complete HTML string, the interior is also using innerHTML.
So it's not completely negative about the use of innerHTML functions.

So from now on, please discard this old knowledge and program using the correct method described below.


about using the HTML DOM to create an element this article does not give a detailed description, here is a simple example:


The first correct way://Using the DOM standard to create the element var select = Document.createelement ("select");    Select.options[0] = new Option ("Add-in 1", "value1");    SELECT.OPTIONS[1] = new Option ("Add-in 2", "value2");    Select.size = "2"; var object = testdiv.appendchild (select);
By using the Document.createelement method, we can create a DOM element and then add it to the specified object through the AppendChild method.

The second way: using jquery


When an HTML string is an element with no attributes, an element is created internally using document.createelement, such as:

Using document.createelement to create elements within jquery:


$ (""). CSS tutorial ("Border", "solid 1px #ff0000"). HTML ("dynamically created Div"). Appendto (Testdiv);
Otherwise use the innerHTML method to create the element:

Using innerHTML to create elements within 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.