编辑 加入regexpr测试解决techfoobar指出得到CSS
问题编辑2 他人HF :)
我正在寻找一个修正码从指定的类获取所有静态样式的方法。我并不想追加一个新的div和因为价值往往只是计算从中提取出所有可能的值...
这里有一些相关的帖子:
jQuery CSS plugin that returns computed style of element to pseudo clone that element?
Can jQuery get all CSS styles associated with an element?
http://quirksmode.org/dom/w3c_css.html
这就是我想出了迄今:
function getExternalCSS(className) {
var cssText = false;
$.each(document.styleSheets, function(key, value) {
$.each(value.cssRules, function(k, v) {
if(new RegExp(className, "i").test(v.selectorText)) {
cssText = v.cssText;
return false;
}
});
if(cssText !== false) {
return false;
}
});
return cssText;
}
个
CSS浏览:
.someClass {
style1...
}
.someOtherClass {
style2...
}
.myClassToFind {
style3...
}
用法:
getExternalCSS(".myClassToFind"); //note the . for class
的selectorText返回正确,但if
永远不会触发。我已经试图解析字符串等任何想法?
我知道这只是一个小版本,它需要更多的改进来覆盖所有的类。但在我的测试用例中,选择器只是.myClassToFind,所以它应该匹配... – ChaosClown 2012-07-20 15:08:06
你不能解释或给出一个链接你的意思是这个PLZ:只计算那些将在最后应用的样式,你可能需要做更多! – ChaosClown 2012-07-20 15:29:25
请检查我的更新以上。我相信这就是你要找的。 – techfoobar 2012-07-20 15:41:49