【问题标题】:How get all style of element [duplicate]如何获得元素的所有样式[重复]
【发布时间】:2021-12-10 14:24:25
【问题描述】:
我有这样的风格:
.filter {
background-image: url("../images/index.png"); !important;
background-blend-mode: difference !important;
filter: invert(1);
box-shadow: inset 0 0 0 1px black;
**other style**
}
我想获得所有可能的风格?
如果我想得到类似的属性名
$(selector).css(property-name:property-value);
我应该为它写很多行代码,因为样式很多。
【问题讨论】:
标签:
javascript
jquery
css
【解决方案1】:
你可以使用getComputerStyle
const h3 = document.querySelector("h3");
const result = document.getComputedStyle(h3).content();
GetComputedStyle Docs
您也可以将getAttribute 方法与style 一起使用
conat h3 = document.querySelector("h3");
let styles = h3.getAttribute("style");
styles.replace(" ", "")
.split(";")
.map(s => s.split(":"));
【解决方案2】:
您无法获取单个 CSS 类的属性,但可以获取元素的所有计算样式:
var e = getElementById('myElement');
console.log( getComputedStyle(e) );