在包含永恒 CSS 样式表(通过 <link rel="stylesheet">
加载)的网页上,我想修改其中一个 CSS 规则的选择器。我可以通过以下方式引用规则:
var rule = document.styleSheets[…].cssRules[…];
(其中 …
是数字)。
选中后,我可以读取它的选择器和其他值:
rule.cssText // 'em { color: red }'
rule.selectorText // 'em'
rule.style.color // 'red'
但是,当我尝试编写其中一个时,它只允许我编写 style
对象:
// These won’t work
rule.cssText = 'em.foo { color: red }';
rule.selectorText = 'em.foo';
// This will work
rule.style.color = 'blue';
为什么它不让我写 .cssText
或 .selectorText
?从我在 the spec 中看到的, 它们不是只读的。
更新:我也检查了内联样式表。它也不起作用。
最佳答案
至少 Firefox 将它们实现为只读。
关于css - 是否可以在外部加载的样式表中修改 CSS 规则的选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020804/