The key code is as follows:
<html> <head> <link rel="stylesheet" type="text/css" href="basic.css"> <script> window.onload=function(){ var sheet=document.styleSheets[0]; //【Add style rules】 // sheet.insertRule('.div1{font-size:16px;color:red;}',0); IE8 and above do not support // The first parameter is the style, the second is the position of the style // sheet.addRule('.div1','font-size:20px;color:orange;font-weight:bold;',0); //IE supports //The first parameter is the style name, the second style position is the style rule, and the third is the style position function insertCss(element,csName,position){ // Cross-browser compatible if(sheet.insertRule){ // sheet.insertRule When it is not IE, a function will be read, and undefined sheet.insertRule(element+'{'+csName+'}',position); }else if(sheet.addRule){ sheet.addRule(element,csName,position); } } insertCss('.div1','font-size:16px; color:orange',0); //【Delete style rules】 //sheet.deleteRule(position); //Delete style parameters are position//IE8 and above do not support //sheet.removeRule(position); //Delete style parameters are position//IE supports function deleteCss(position){ //Cross-browser-compatible if(sheet.deleteRule){ sheet.deleteRule(position); }else if(sheet.removeRule){ sheet.removeRule(position); } deleteCss(0); } } </script> </head> <body></body></html>The above is the new JavaScript style rules (recommended) introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!