【发布时间】:2013-05-05 03:42:47
【问题描述】:
我的网页中有多个style 标签,我想在其中操作cssRules。如何从样式元素中获取styleSheet 中的document.styleSheets 对象。一种方法是扫描document.styleSheets 中的所有styleSheets,并将其ownerNode 与我的style 元素对象匹配。有没有更好的办法呢?
【问题讨论】:
标签: javascript cssom
我的网页中有多个style 标签,我想在其中操作cssRules。如何从样式元素中获取styleSheet 中的document.styleSheets 对象。一种方法是扫描document.styleSheets 中的所有styleSheets,并将其ownerNode 与我的style 元素对象匹配。有没有更好的办法呢?
【问题讨论】:
标签: javascript cssom
根据 http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-style-element ,样式元素实现了 LinkStyle 接口,通过遵循引用可以将一个引导到 http://dev.w3.org/csswg/cssom/#the-linkstyle-interface,这表明您可以为每个样式元素访问 sheet 属性(与 @ 987654325@)--只要是text/css,默认即可。从那里您可以获得您正在寻找的更专业的 CSSStyleSheet 界面(即带有cssRules 的界面)。
<style>
p {color:blue;}
</style>
<script>
var h = document.getElementsByTagName('style')[0];
alert(h.sheet.cssRules[0].cssText); // "p { color: blue; }"
</script>
【讨论】: