JS Dynamic Create style node (JS file to add CSS)

Source: Internet
Author: User

IE6 cannot document.createelement (' style ') and then append into the head tag. So I found such a good article.

---------------------

There are many ways to create a style node dynamically, but most are limited to external CSS files. How can I dynamically create a style node using a program-generated string that I've been doing for 2 hours.

static external CSS file syntax:

@import URL (style.css);

Dynamic external CSS files are loaded in the following ways:

The first type:

var style = document.createelement (' link ');
Style.href = ' style.css ';
Style.rel = ' stylesheet ';
Style.type = ' text/css ';
document.getElementsByTagName (' HEAD '). Item (0). appendchild (style);

The second type is simple:

Document.createstylesheet (STYLE.CSS);

A dynamic style node that uses a program-generated string:

var style = document.createelement (' style ');
Style.type = ' text/css ';
Style.innerhtml= "body{Background-color:blue;}";
document.getElementsByTagName (' HEAD '). Item (0). appendchild (style);

Unfortunately, the above code is successful in FF, but IE does not support it. Get the code from the Foreigner forum:

var sheet = Document.createstylesheet ();
Sheet.addrule (' body ', ' background-color:red ');

Success, but very troublesome, to take the string apart, a long write dead, tired like a dog.

Keep searching, find the code on a blog that doesn't know what language the country is in:

Document.createstylesheet ("javascript: ' Body{background-color:blue; '");

Success, this person is really bad, but the problem comes out, the URL maximum 255 characters, a little longer will not, change:

Window.style= "Body{background-color:blue;";
Document.createstylesheet ("Javascript:style");

Perfect solution!! Code:

<script>
function Blue () {
if (document.all) {
Window.style= "body{";
Document.createstylesheet ("Javascript:style");
}else{
var style = document.createelement (' style ');
Style.type = ' text/css ';
Style.innerhtml= "body{Background-color:blue}";
document.getElementsByTagName (' HEAD '). Item (0). appendchild (style);
}
}
</script>
<body>
<input type= "button" value= "Blue" onclick= "Blue ();" />
</body>

Original http://www.cnblogs.com/stephenykk/archive/2013/06/10/3131231.html

JS Dynamic Create style node (JS file to add CSS)

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.