How to dynamically change the style tag using JavaScript?

前端 未结 9 2169
走了就别回头了
走了就别回头了 2021-01-01 21:50

I want to change the style tag after generating contents. How can I add style to the style tag? I tried using:

document.getElementById(\'style\').appendChil         


        
9条回答
  •  情深已故
    2021-01-01 22:38

    I tossed together a quick example at http://jsfiddle.net/fttS5/1/ .

    You can attach an id to the style tags just like you would any other element in HTML.

    Now you can take the code you tried with appendChild and add one quick change to it

    document.getElementById('style').appendChild(document.createTextNode(styleContents));

    This should do the trick. Good Luck.

    You can also just use the innerHTML method listed as the answer below mine.

提交回复
热议问题