【发布时间】:2011-11-25 22:05:59
【问题描述】:
我的样式表中有 3 块样式规则。 1 个 ID 选择器 2个类选择器
从 CSSStyleSheet 查询 cssRules 后,它只返回与类选择器相关的样式。如何从外部样式表中获取id相关的样式?
注意:我使用的是 firefox。
var style_rules = document.styleSheets[0].cssRules;
console.log(style_rules.length); //2
for(var i=0; i < style_rules.length; i++) {
console.log(style_rules[i].selectorText);
console.log(style_rules[i].style.cssText);
}
CSS 样式表:
//@import 'reset.css';
#content {
position:absolute;
top:200px;
left:200px;
height:200px;
width:200px;
}
.red {
background-color:red;
}
.green{
color:yellow;
background-color:green;
}
更新:如果我删除评论 @import 它对我来说很好。好像是bug。
【问题讨论】:
-
请将 URL 发布到该外部 CSS 文件。
-
它对我有用:jsfiddle.net/u4v63/2
-
我在本地主机上使用 。
-
更新:如果我删除评论 @import 它对我来说很好。似乎是错误。
-
//在 CSS 中无效。要在 CSS 中发表评论,请使用/*和*/...
标签: javascript css firefox