[英]How do I return all CSS properties applied to an element by an external stylesheet class (not get computed styles!)?
Given css class: 给定CSS类:
.red {
color: rgb(255,0,0);
background-color: rgb(0, 255, 0);
}
I then add a div to the dom via: 然后,我通过以下方式将div添加到dom:
$("<div></div>").addClass("red").hide().appendTo("body");
I can get single properties using: 我可以使用以下方法获取单个属性:
$(".red").css("color");
Which returns rgb(255, 0, 0); 返回rgb(255,0,0);
My question is how can I loop through the css class properties and return them all without directly using the property name? 我的问题是如何在css类属性中循环并返回它们,而无需直接使用属性名?
I've actually found a solution using this very handy jQuery library: https://github.com/f0r4y312/jquery-stylesheet 我实际上已经找到了使用此非常方便的jQuery库的解决方案: https : //github.com/f0r4y312/jquery-stylesheet
I can now loop through each stylesheet and the declaration and return: 我现在可以遍历每个样式表和声明并返回:
var key is a loop through the css property names
$.stylesheet("." + key + "").rules()[0].style
Which then returns all the style properties: 然后返回所有样式属性:
0 "color" "rgb(255, 0, 0)"
1 "background-color" "rgb(0, 255, 0)"
etc
Which I'm now serialising into a json array for a unit test! 我现在将其序列化为一个json数组以进行单元测试!
Thanks for all your very helpful replies! 感谢您的所有非常有帮助的回复!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.