How to load in an external CSS file dynamically?

后端 未结 8 1655
滥情空心
滥情空心 2020-12-14 22:32

I\'m trying to create a dynamic page using external .css pages where the page color will get changed. Below is my code. But when I click the href, I am not gett

相关标签:
8条回答
  • 2020-12-14 22:59

    Try this:

    var fileref = document.createElement("link");
    fileref.rel = "stylesheet";
    fileref.type = "text/css";
    fileref.href = "filename";
    document.getElementsByTagName("head")[0].appendChild(fileref)
    
    0 讨论(0)
  • 2020-12-14 23:02

    That code should work, except perhaps you might want to add type="text/javascript" to your script tag.

    I tested the code via Firebug and it worked for me.

    Maybe the problem is that your original stylesheet is still included in the page and thus overriding the style of your secondary stylesheet?

    0 讨论(0)
提交回复
热议问题