In-depth understanding of scripted CSS series fifth-dynamic styles

Source: Internet
Author: User

Previous words

Most of the time, DOM operations are straightforward, so it is not cumbersome to generate content that is normally generated from HTML code with Javascript. But because browsers are riddled with hidden traps and incompatibilities, it's more complicated to deal with parts of the dom, such as dynamic styles that are relatively complex

A dynamic style is a style that does not exist when the page is loaded and is dynamically added to the page after the page is Loaded.

Dynamic styles include two Situations: one is to insert an external style sheet through the <link> element, and the other is to insert an inner style through the <style> Element. These two scenarios are described in more detail below

External styles
/* style.css inside the content */ . Box{height:100px;width:100px;background-color:pink;}
var link = document.createelement ("link"= "stylesheet"= "text/css"= "style.css" ; var head = document.getElementsByTagName (' head ') [0];head.appendchild (link);

Use the functions to encapsulate the following:

<div class= "box" > Test text </div><button id= "btn" > Dynamic Add Style </button><script>functionloadstyles (url) {loadstyles.mark= ' Load '; varlink = document.createelement ("link"); Link.rel= "stylesheet"; Link.type= "text/css"; Link.href=url; varHead = document.getElementsByTagName (' head ') [0]; Head.appendchild (link); }btn.onclick=function(){    if(loadstyles.mark! = ' Load ') {loadstyles ("style.css"); }}</script>

Internal style
var style = document.createelement ("style"= "text/css"= ". box{height:100px;width:100px; background-color:pink;} " ; var head = document.getElementsByTagName (' head ') [0

Use the functions to encapsulate the following:

<div class= "box" > Test text </div><button id= "btn" > Dynamic Add Style </button><script>functionloadstyles (str) {loadstyles.mark= ' Load '; varstyle = Document.createelement ("style"); Style.type= "text/css"; Style.innerhtml=str; varHead = document.getElementsByTagName (' head ') [0]; Head.appendchild (style); }btn.onclick=function(){    if(loadstyles.mark! = ' Load ') {loadstyles (". box{height:100px;width:100px;background-color:pink;}"); }}</script>

[note] This method errors in the Ie8-browser because the Ie8-browser treats <style> as a special node, does not allow access to its child nodes, or sets the innerHTML property

Compatible

IE browser supports accessing and modifying the Csstext property of an element's cssstylesheet object, enabling similar effects by modifying this property

<div class= "box" > Test text </div><button id= "btn" > Dynamic Add Style </button><script>functionloadstyles (str) {loadstyles.mark= ' Load '; varstyle = Document.createelement ("style"); Style.type= "text/css"; Try{style.innerhtml=str; }Catch(ex) {style.styleSheet.cssText=str; }    varHead = document.getElementsByTagName (' head ') [0]; Head.appendchild (style); }btn.onclick=function(){    if(loadstyles.mark! = ' Load ') {loadstyles (". box{height:100px;width:100px;background-color:pink;}"); }}</script>

In-depth understanding of scripted CSS series fifth-dynamic styles

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.