Dynamically create and access page elements

Source: Internet
Author: User

Recently, the OCX control is being made, but this control is not an open web page that needs to be initialized, but is dynamically created as needed, when the control is initialized, it needs to get its DOM object in the Web page, and the method to invoke the control immediately after creation.

The first method is as follows:

1 var ctrl = Document.createelement (' object '); 2 ctrl.classid = ' clsid:dcc16727-7a51-47b4-b38e-b020eaaf0584 '; 3 document.body.appendChild (ctrl);

This method does not have any problems in IE11, but in IE9, the control is initialized when it gets its DOM object in the Web page.

By testing, adding the appropriate HTML code directly to the page can be obtained at the time the control is initialized to its DOM object in the Web page.

<HTML><HEAD><TITLE>ATL test page for Object Test3</TITLE></HEAD><BODY><OBJECTID= "Test3"CLASSID= "clsid:dcc16727-7a51-47b4-b38e-b020eaaf0584"></OBJECT></BODY></HTML>

Search on the Internet, found such an article, "FAQ: How to dynamically create and access page elements," his idea is in the OCX with the insertAdjacentHTML method created. This method does not solve the problem of "the control does not get its DOM object in the Web page when it is initialized."

Reading the idea of this article, reminds me of the previous view of the Dom Bridge, Reflow and redraw concept (refer to "high-performance JavaScript DOM programming and Reflow and Redraw"), call getElementById will cause the bridge, the bridge will cause reflow and redraw.

Based on this concept, you want to create the control through innerHTML, is not the same as the first to write the HTML code is the same. The final test, this method is feasible.

The final code is as follows:

var div = document.createelement (' div '= ' <object id= "Test4" classid= "clsid:d cc16727-7a51-47b4-b38e-b020eaaf0584 "></OBJECT>"= document.getElementById (' Test4 ');

How to get its DOM object in a Web page when the control is initialized, refer to my other article, "getting its DOM object in a Web page when OCX is initialized."

Dynamically create and access page elements

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.