小编use*_*123的帖子

'cssRules'和'rules'对象有什么区别?

有什么区别:

document.styleSheets[0].cssRules
Run Code Online (Sandbox Code Playgroud)

document.styleSheets[0].rules
Run Code Online (Sandbox Code Playgroud)

我注意到第二个也得到了IE8和之前的支持.但是这两个对象有什么区别?

javascript css document

14
推荐指数
2
解决办法
1148
查看次数

Firefox 和 IE 无法修改 cssRules

我需要更改现有的全局 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)

javascript css firefox internet-explorer cross-browser

5
推荐指数
1
解决办法
484
查看次数