How to load up CSS files using Javascript?

后端 未结 19 3270
栀梦
栀梦 2020-11-21 07:51

Is it possible to import css stylesheets into a html page using Javascript? If so, how can it be done?

P.S the javascript will be hosted on my site, but I want users

19条回答
  •  迷失自我
    2020-11-21 08:11

    I know this is a pretty old thread but here comes my 5 cents.

    There is another way to do this depending on what your needs are.

    I have a case where i want a css file to be active only a while. Like css switching. Activate the css and then after another event deativate it.

    Instead of loading the css dynamically and then removing it you can add a Class/an id in front of all elements in the new css and then just switch that class/id of the base node of your css (like body tag).

    You would with this solution have more css files initially loaded but you have a more dynamic way of switching css layouts.

提交回复
热议问题