WEB开发网
开发学院网页设计JavaScript JS 获取指定类别选择器的所以样式值 阅读

JS 获取指定类别选择器的所以样式值

 2012-11-21 14:07:47 来源:WEB开发网   
核心提示: /** 获取指定类别选择器的所以样式值* @method getClassStyle* @param {String} name 类别选择器的名字* @param {Document} doc 指定的document* @return {String} 该样式的cssText*/getClass

  /*
* 获取指定类别选择器的所以样式值
* @method getClassStyle
* @param {String} name 类别选择器的名字
* @param {Document} doc 指定的document
* @return {String} 该样式的cssText
*/
getClassStyle : function(name, doc) {
var doc = doc || document,
name2 = name.replace(/\./, '\\.').replace(/\b/,'\\b') + '\\b',
reg = new RegExp(name2, 'i');
ret = '';
for(var i=0; i<doc.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i],
rules = styleSheet.cssRules ? styleSheet.cssRules : styleSheet.rules;
for(var j=0; j<rules.length; j++) {
var rule = rules[j],
cls = rule.selectorText;
// rule.selectorText可能不存在
if(cls && reg.test(cls)) {
var newStr = /{.*?}/.exec(rule.cssText) || '';
// 去重
if(-1 == ret.indexOf(newStr)) {
ret += newStr;
}
}
}
}
// 过滤掉大括号
return ret.replace(/{|}/g,'');
}

Tags:JS 获取 指定

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接