JavaScript获取指定名字的样式规则

阅读更多
function getCSSRule(ruleSelectorText, styleSheetHref) {
	var doc = document,
		styleSheets = doc.styleSheets,
		rules = [],
		styleSheet, rule,
		i, j, len, len2;
	for (i = 0, len = styleSheets.length; i < len; i++) {
			styleSheet = styleSheets[i];
			if (styleSheetHref) {
				if (styleSheet.href.indexOf(styleSheetHref) === -1) {
					continue;
				}
			}
			for (j = 0, len2 = styleSheet.rules.length; j < len2; j++) {
				rule = styleSheet.rules[j];
				if (rule.selectorText === ruleSelectorText) {
					return rule;
				}
			}
	}
	return null;
}


参数说明:
ruleSelectorText
样式类名,比如".myDiv", ".test"

styleSheetHref
样式文件的URL, 比如http://localhost/myWebApp/resources/my.css

用法举例:
getCSSRule(".x-button")
返回样式".x-button" object.

你可能感兴趣的:(style,css,rule,样式,规则)