![](/img/trans.png)
[英]How can I get all the properties from the class or id on click using jQuery?
[英]How can I get ALL class properties in JavaScript
是否有可能通过JavaScript获得类的所有属性? 可以说我有一堂课
.menu { color: black; width: 10px; }
如何通过JavaScript以字符串形式获取“ color:black; width:10px; ”?
谢谢!
您可以使用getComputedStyle()。 这将找到在css文件中完成的所有内联样式或css样式。这将返回元素计算的所有属性。
请参见下面的代码段
var el=document.getElementsByClassName("menu")[0]; style=getComputedStyle(el); console.log(style);
.menu{ color:green; background:blue; opacity:1 }
<div class="menu"></div>
您还可以从上述代码段的类中获取特定属性,例如
style.width
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.