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 }");