<style type=”text/css”>
a {color:#0f0;}
</style>
<script type=”text/javascript”>
if(document.styleSheets[0].cssRules){
alert(document.styleSheets[0].cssRules[0].style.cssText) //FF
}else{
alert(document.styleSheets[0].rules[0].style.cssText); //IE
}
</script>
a {color:#0f0;}
</style>
<script type=”text/javascript”>
if(document.styleSheets[0].cssRules){
alert(document.styleSheets[0].cssRules[0].style.cssText) //FF
}else{
alert(document.styleSheets[0].rules[0].style.cssText); //IE
}
</script>
IE6下弹出 COLOR:#0f0
FF下弹出 color: rgb(0,255,0);
如果把a {color:#0f0;}改成a {color:green;}
IE6下弹出 COLOR:#green
FF下弹出 color: green;
而且 rules 和 cssRules 的计数方法也是不一样的!rules 是第几个选择器;cssRules 是第几条规则。分别用 IE 和 firefox 运行下面的代码可知。
<style type=”text/css”>
a,div,p {color:red;}
h5 {color:blue;}
</style>
<script type=”text/javascript”>
if(document.styleSheets[0].cssRules){
alert(document.styleSheets[0].cssRules[1].selectorText+”:”+document.styleSheets[0].cssRules[1].style.cssText)
}else{
alert(document.styleSheets[0].rules[1].selectorText+”:”+document.styleSheets[0].rules[1].style.cssText)
}
</script>
a,div,p {color:red;}
h5 {color:blue;}
</style>
<script type=”text/javascript”>
if(document.styleSheets[0].cssRules){
alert(document.styleSheets[0].cssRules[1].selectorText+”:”+document.styleSheets[0].cssRules[1].style.cssText)
}else{
alert(document.styleSheets[0].rules[1].selectorText+”:”+document.styleSheets[0].rules[1].style.cssText)
}
</script>