简体   繁体   中英

dynamic CSS loading in IE won't work

I have a button in my page, and onclick event, if should append to the <head> a CSS file from a server and do something else. it works perfectly in FF but in IE, it seems not to work (it did append the <link> to the <head> - but the CSS won't affect the elements)

Heres my current code:

function loadDynamicCss(filename) {
    var fileref = document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
    document.getElementsByTagName("head")[0].appendChild(fileref);
}

What can cause this?

Thanks!

Try this function:

function include_css(url) {
var page = document.getElementsByTagName('head')[0],
        cssElem = document.createElement('link');

        cssElem.setAttribute('rel', 'css');
        cssElem.setAttribute('type', 'text/css');
        cssElem.setAttribute('href', url);

        page.appendChild(cssElem);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM