This article mainly introduces javascript Two methods of dynamically generating css code. Sometimes we need to use js to dynamically generate css code in the style tag on the page. Here are two methods to introduce to you Methods, friends in need can refer to
javascript Two methods of dynamically generating css code
Sometimes we need to use js to dynamically generate the style tag in the page css code, the method is very straightforward, that is, directly create a style element, then set the css code in the style element, and finally insert it into the head element. But there are some compatibility issues we need to resolve. First of all, in browsers that comply with the w3c standard, we only need to insert the css code to be inserted into the style element as a text node. In IE, we need to use the styleSheet.cssText of the style element. solve. It should also be noted that in some versions of IE, the number of style tags on a page is limited. If it is exceeded, an error will be reported. This needs to be considered.
Method 1:
Add an id name to the