JavaScript insert dynamic style implementation code

Source: Internet
Author: User

Similar to dynamic scripts, a dynamic style is a style that does not exist when the page is loaded; a dynamic style is dynamically added to the page after the page is loaded.

The following typical <link> element is used as an example:

<Link rel = "stylesheet" type = "text/css" href = "style.css"> using DOM code, you can easily dynamically create this element:
Copy codeThe Code is as follows:
Var link = document. createElement ("link ");
Link. rel = "stylesheet ";
Link. type = "text/css ";
Link. href = "style.css ";
Var head = document. getElementsByTagName ("head") [0];
Head. appendChild (link );

The above code can run normally in all mainstream browsers. Note that the <link> element must be added to the Copy codeThe Code is as follows:
Function loadStyles (url ){
Var link = document. createElement ("link ");
Link. rel = "stylesheet ";
Link. type = "text/css ";
Link. href = url;
Var head = document. getElementsByTagName ("head") [0];
Head. appendChild (link );
}
LoadStyles ("style.css ")

The process of loading external style files is asynchronous, that is, there is no fixed order between the process of loading styles and executing JavaScript code.

Another way to define a style is to use the <style> element to include embedded CSS, as shown below:

<Style>
Body {background-color: red ;}
</Style>

The following DOM code should be valid according to the same logic:
Copy codeThe Code is as follows:
Var style = document. createElement ("style ");
Style. type = "text/css ";
Style. appendChild (document. createTextNode ("body {background-color: red ;}"));
Var head = document. getElementsByTagName ("head") [0];
Head. appendChild (style );

The above code can be run in Firefox, Safrai, Chrome, and Opera, and an error will be reported in IE. IE regards <style> as a special node similar to <script> and does not allow access to its subnodes. In fact, the error thrown by IE is the same as the error thrown when a subnode is added to the <script> element. The solution to this problem in IE is to access the styleSheet attribute of the element, which has another cssText attribute and can accept CSS code, as shown in the following example:
Copy codeThe Code is as follows:
Var style = document. createElement ("style ");
Style. type = "text/css ";
Try {
Style. appendChild (document. createTextNode ("body {background-color: red }"));
} Catch (ex ){
Style.styleSheet.css Text = "body {background-color: red }";
}
Var head = document. getElementsByTagName ("head") [0];
Head. appendChild (style );

Similar to dynamically adding embedded scripts, the rewritten Code uses the try-catch statement to capture the error thrown by IE, and then sets the style in a special way for IE. A common solution is as follows:
Copy codeThe Code is as follows:
Function loadStyleString (css ){
Var style = document. createElement ("style ");
Style. type = "text/css ";
Try {
Style. appendChild (document. createTextNode (css ));
} Catch (ex ){
Style.styleSheet.css Text = css;
}
Var head = document. getElementsByTagName ("head") [0];
Head. appendChild (style );
}
LoadStyleString ("body {background-color: red }");

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.