JS控制CSS样式的方法 时间:2020-05-12 人气:0 群里的兄弟问的效果 无标题文档 <script language="javascript"> function remove(){ var wc = document.styleSheets[0], i; for (i = 0 ; i < wc.rules.length ; i ++) wc.removeRule(i); } </script> hello,this is a example of stylesheets hello,this is a example of stylesheets [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]网上搜到的不错的控制css JS控制样式 <script type="text/javascript"> (function () { if (window.CSSStyleSheet) { var wc = window.CSSStyleSheet.prototype; wc.addRule = function (a, b) { var wc = this; wc.insertRule(a + "{" + b + "}", wc.cssRules.length); }; wc.removeRule = function (a) { var wc = this; wc.deleteRule(a); }; wc.__defineGetter__("rules", new Function("return this.cssRules")); } })(); var addRule = function (a, b) { var wc = document.styleSheets[0], i; for (i = 0 ; i < wc.rules.length ; i ++) if (wc.rules[i].selectorText == a) wc.removeRule(i); wc.addRule(a, b); }; var add = function () { var wc = document.getElementById("wc").value; if (/([^\{]+)\s*(\{[^}]+\})/.test(wc)) { addRule(RegExp.$1, RegExp.$2); } else { alert("样式错误"); } }; </script> 我是字 控制 .font { font-size:18px; font-weight:bold; line-height:20px; color:#FF0000; } </textarea> [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容