1. Simple method, regardless of it, just do it like this:
document.createStyleSheet().cssText = 'Tag{color:red;' + // This comment only helps understand in the current JS and will not be written to CSS 'width:300px;height:150px}' + '.Class name {...}' + '#IDs {...}';//Finish. I like to write semicolons like this, which is better aligned with the starting position of the instruction writing, especially when there are other statements behind it.
2. Improve a little more to prevent repeated additions. You can achieve it by adding style sheet ID and judging it:
if (!document.styleSheets['The stylesheet ID to be created is like theforever']) { //First check whether the stylesheet ID to be created exists to prevent repeated addition of var ss = document.createStyleSheet(); ss.owningElement.id = 'The stylesheet ID to be created is like theforever'; ss.cssText = 'Tag{display:inline-block;overflow:hidden;' + // This comment only helps understand in the current JS and will not be written to CSS' text-align:left;width:300px;height:150px}' + '.Classname {...}' + '#IDs{...}' ; }The above method of dynamically building or adding CSS style sheets with JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.