|
<script type= "Text/javascript" >
/* ###################################################
* Description: Use JavaScript to get the CSS rules with selectors on the page
* includes ' internal style blocks ' and ' external style sheet files '
* Author: Yanfu Xie [xieyanfu@yahoo.com.cn]
* Website: http://www.111cn.net
* Date: 2007.01.16
* #################################################*/
function Getcssrule ()
{
Total Styles
var stylesheetlen = document.styleSheets.length;
if (!stylesheetlen) return;
Style rule name, IE and FireFox are different
var rulename = (document.stylesheets[0].cssrules)? ' Cssrules ': ' Rules '; Firefox:cssrules | | Ie:rules
Initial results
var result = {};
var totalrulelen = 0;
var totalselectorlen = 0;
var totalproperty = 0;
for (var i=0; i<stylesheetlen; i++)
{
Get each style sheet definition
var styleSheet = document.stylesheets[i];
Number of rules per style sheet
var rulelen = stylesheet[rulename].length;
Totalrulelen + = Rulelen;
for (var j=0; j<rulelen; J + +)
{
Gets the selector for the current rule
Convert incoming selector to lowercase
var selectors = Stylesheet[rulename][j].selectortext.tolowercase (). Split (",");
var selectorlen = selectors.length;
Totalselectorlen + = Selectorlen;
for (var s=0; s<selectorlen; s++)
{
Remove the spaces around the selector
Selector = Selectors[s].replace (/(^s*) | ( s*$)/g, "");
Initializes the current selector
Result[selector] = {};
Get the current style
var styleset = Stylesheet[rulename][j].style;
For (property in Styleset)
{
Get rules
if (Styleset[property] && property!= ' Csstext ')
{
Alert (selector + ' => ' + property + ': ' + Styleset[property])
Result[selector][property] = Styleset[property];
Totalproperty + 1;
}
}
}
}
}
Statistical data
Result.totalsheet = Stylesheetlen; Style block
Result.totalrule = Totalrulelen; Number of rules
Result.totalselector = Totalselectorlen; Selection character
Result.totalproperty = Totalproperty; Property
return result;
}
</script>
|