Notes for createElement in IE

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows:
Var $ alertPanel = $ (document. createElement ("div "));
Export alertpanel.css ("width", "120px" ).css ("height", "50px"). text ("Hello CssRain! ");
Parameters ('body', parent.doc ument). append ($ alertPanel );

According to his meaning, I also wrote a Demo and found that it was indeed like this.
I did not see any similar problems after turning over the information.
Then I wrote it once using the native DOM method and found that it would not work either.
Copy codeThe Code is as follows:
Var div = document. createElement ("div ");
Div. style. width = "120px ";
Div. style. height = "50px ";
Div. style. border = "solid 1px #000000 ";
Div. innerHTML = "Hello CssRain! ";
Parent.doc ument. body. appendChild (div );

If you want to appendchildto parent.document, what about parent.doc ument. createElement when you create the appendchildfile?
So I changed the code:
Copy codeThe Code is as follows:
Var div = parent.doc ument. createElement ("div ");
Div. style. width = "120px ";
Div. style. height = "50px ";
Div. style. border = "solid 1px #000000 ";
Div. innerHTML = "Hello CssRain! ";
Parent.doc ument. body. appendChild (div );

In this way, IE6 and IE7 can be used successfully.
Example:
Demo address: http://demo.jb51.net/js/IE-createElement/page1.htm
Summary:

If you want to create a parent page element in IE6 and IE7, you must make the creation element belong to the parent page.
Copy codeThe Code is as follows:
Var dummy = parent.doc ument. createElement ("div ");
Var t = parent.doc ument. createElement ("table ");


In Firefox and IE8, it allows you to create elements to be appended to another document in one document.
In firefoxand ie8, you can use parent.doc ument or document.

In addition, google's browser is very weird and messy. If you want to be compatible with the google browser, we recommend that you use the parent. Function Name () to call the parent page.

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.