【发布时间】:2016-11-26 15:23:44
【问题描述】:
我需要更改现有的全局 CSS 规则,然后访问 document.styleSheets,获取我的规则并对其进行修改。
我通过访问 selectorText 属性来修改选择器。
CSS 代码
<style type="text/css">
.class {
color: #230020;
}
</style>
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.
我的问题是在所有版本的 Firefox 和 Internet Explorer 中,属性 selectorText 似乎是只读的。
/* Internet Explorer, Edge, and Firefox */
rule.selectorText; // returns ".class"
rule.selectorText = ".another-class";
rule.selectorText; // returns ".class", It remains as it was.
如何让它在 Mozilla Firefox、Microsoft Internet Explorer 和 Edge 上运行?
【问题讨论】:
标签: javascript css internet-explorer firefox cross-browser