简体   繁体   English

如何获取JavaScript中的所有类属性

[英]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.

相关问题 如何使用jQuery单击该类或ID的所有属性? - How can I get all the properties from the class or id on click using jQuery? 如何使用javascript获取输入复选框标签的所有默认CSS属性? - how can i get all default css properties of input checkbox tag using javascript? 我怎样才能让这个javascript函数一直工作? (未捕获的 TypeError:无法读取 null 的属性) - How can I get this javascript function to work all of the time? (Uncaught TypeError: Cannot read properties of null) 如何在Chrome中显示JavaScript数组/对象的所有属性? - How can I display all properties of a JavaScript array/object in Chrome? 如何获取JavaScript中的所有计时器? - How can I get all timers in javascript? 如何获取 object 中所有相同属性的值? - How can I get the value of all the same properties in an object? 如何列出给定对象的所有 get 属性? - How can I list all the get properties from a give object? 如何在 javascript 中获取 CSS 媒体属性 - How can I get CSS media properties in javascript 如何使用javascript和/或jQuery获取特定类中所有元素的数组? - How can I get an array of all elements within a specific class using javascript and/or jQuery? 如何在javascript中获取文件对象的所有属性? - How to get all properties of a File Object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM