<style>
.aa { color:#f00 }
.bb { font-weight:bold }
</style> <SCRIPT LANGUAGE="JavaScript">
<!-- var a = document.styleSheets[0];
for(var i=0;i<a.rules.length;i++){
alert(a.rules[i].selectorText);
} //-->
</SCRIPT>
.aa { color:#f00 }
.bb { font-weight:bold }
</style> <SCRIPT LANGUAGE="JavaScript">
<!-- var a = document.styleSheets[0];
for(var i=0;i<a.rules.length;i++){
alert(a.rules[i].selectorText);
} //-->
</SCRIPT>
<HTML>
<HEAD>
<SCRIPT>
/** Mozilla兼容MsIE脚本,stylesheet扩展部分。
* o stylesheet.addRule()
*/
(function () {
if (! window.CSSStyleSheet) return;
function _ss_GET_rules_ () {
return this.cssRules;
}
var _ss = CSSStyleSheet.prototype;
_ss.addRule = function(sSelector, sRule) {
this.insertRule(sSelector + "{" + sRule + "}", this.cssRules.length);
}
_ss.__defineGetter__("rules", _ss_GET_rules_);
})();function ruleColor(ruleIndex) {
alert("The color of rule " + ruleIndex + " is " + document.styleSheets[0].rules.item(ruleIndex).style.color + ".");
}
</SCRIPT>
<STYLE>
.rule0 {color:red;}
.rule1 {color:blue;}
</STYLE>
</HEAD>
<BODY>
<P class="rule0" id="oRule0Span">
Rule 0 is applied to this line.
</P>
<P class="rule1" id="oRule1Span">
Rule 1 is applied to this line.
</P>
<BUTTON onclick="ruleColor(0)">Color of Rule 0</BUTTON>
<BUTTON onclick="ruleColor(1)">Color of Rule 1</BUTTON>
</BODY>
</HTML>
alert("The color of rule " + ruleIndex + " is " + document.styleSheets[0].rules.item(ruleIndex).style.color + ".");-->alert("The name of rule " + ruleIndex + " is " + document.styleSheets[0].rules.item(ruleIndex).selectorText + ".");
请问有兼容性更好点的吗?opera不支持 ;(
opera没环境 ^_^
<script type="text/javascript">
var getStyle = function () {
var style = document.getElementsByTagName("style")[0];
var sp = /(^|\s)(.+)\s\{[^}]*\};*/g, ar = new Array();
while (sp.exec(style.innerHTML))
Array.prototype.push.apply(ar, RegExp.$2.split(/[ ]*,[ ]*/));
return ar;
};
alert(getStyle().join("\n"));
</script>
.aa { color:#f00 }
.bb { font-weight:bold }
#wc { color:#000000
}
#my, aa, bb cc,w:cs { color:#000000}
</style>
css忘记贴了。。
在修改下式子...