有什么区别:
document.styleSheets[0].cssRules
Run Code Online (Sandbox Code Playgroud)
和
document.styleSheets[0].rules
Run Code Online (Sandbox Code Playgroud)
我注意到第二个也得到了IE8和之前的支持.但是这两个对象有什么区别?
我需要更改现有的全局 CSS 规则,然后访问document.styleSheets,获取我的规则并修改它。
我通过访问selectorText属性来修改选择器。
CSS代码
<style type="text/css">
.class {
color: #230020;
}
</style>
Run Code Online (Sandbox Code Playgroud)
JavaScript 代码
var rule = document.styleSheets[0].cssRules[0]; // obtain the first rule.
/* Chrome, Opera, Safari */
rule.selectorText; // returns ".class"
rule.selectorText = ".another-class";
rule.selectorText; // returns ".another-class", then it works and the rule changed.
Run Code Online (Sandbox Code Playgroud)
我的问题是在所有版本的 Firefox 和 Internet Explorer 中,属性selectorText似乎是只读的。
/* Internet Explorer, Edge, and Firefox */
rule.selectorText; // returns ".class"
rule.selectorText = ".another-class";
rule.selectorText; // returns ".class", It …Run Code Online (Sandbox Code Playgroud)