[英]How can I get ALL class properties in JavaScript
Is it somehow possible to get all properties of a class through JavaScript? 是否有可能通过JavaScript获得类的所有属性? Lets say I have a class 可以说我有一堂课
.menu { color: black; width: 10px; }
How can I get " color: black; width: 10px; " as a string through JavaScript? 如何通过JavaScript以字符串形式获取“ color:black; width:10px; ”?
Thank you! 谢谢!
You can use getComputedStyle(). 您可以使用getComputedStyle()。 This will find all inline-style or css styling done in a css file..This returns all properties computed of the element 这将找到在css文件中完成的所有内联样式或css样式。这将返回元素计算的所有属性。
See snippet below 请参见下面的代码段
var el=document.getElementsByClassName("menu")[0]; style=getComputedStyle(el); console.log(style);
.menu{ color:green; background:blue; opacity:1 }
<div class="menu"></div>
You can also get a specific property from the class in the above snippet, example 您还可以从上述代码段的类中获取特定属性,例如
style.width
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.