How to load in an external CSS file dynamically?

后端 未结 8 1657
滥情空心
滥情空心 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:51

    Tim Goodman's answer is correct, but instead of fileref.href = "filename"; it should be fileref.href = filename;

    Otherwise you're trying to load a file with the name "filename" rather than what you passed to the script.

    Alternately, if you're willing to use the jQuery library, this can be accomplished in one line:
    $("head").append("");

    Also, about the first version of your script using setAttribute: Most browsers will only take setAttribute with an empty third parameter because of the way the spec is set up:
    fileref.setAttribute("href", filename, "");

提交回复
热议问题