1, the simple method, regardless, directly this can be:
Document.createstylesheet (). Csstext = ' tag {color:red; ' +
//This comment only helps to understand in current JS and will not be written in CSS
' width:300px;height : 150px} ' +
'. Class name {...} ' +
' #ID们 {...} '
;
Finish the job. I like the semicolon to write, and it's better to align it with the beginning of the instruction, especially when there are other statements behind it.
2, a better way to prevent repeated additions, you can add a Stylesheet ID and judge it to achieve:
if (!document.stylesheets[' to create a style sheet ID such as Theforever ']) {//first check to see if the style sheet ID you want to establish exists to prevent the repeated addition of the
var ss = Document.createstylesheet ();
Ss.owningElement.id = ' to be established style sheet ID such as Theforever ';
Ss.csstext = ' Tag {display:inline-block;overflow:hidden ' +
//This annotation only helps understand in current JS, and will not be written into CSS
' text-align:left; width:300px;height:150px} ' +
'. Class name {...} ' +
' #ID们 {...} '
;
}
The above article with JavaScript dynamic build or add CSS style sheet implementation method is small set to share all the content, hope to give you a reference, also hope that we support cloud habitat community.