【发布时间】:2019-01-23 06:54:32
【问题描述】:
好的,我完全期望因为提出一些愚蠢的问题(或至少是重复的)而被火焚烧,但是在附加的 sn-p 中,为什么我必须使用 window.getComputedStyle 来访问 CSS 应用的样式?我的印象是,.style 字段至少会反映 CSS 最初应用的那些样式,和/或此后手动更改的样式。
如果不是,控制哪些属性(以及何时)反映在元素的 .style 字段中的确切规则是什么?
setTimeout(() => {
console.log("the bckg color:", reddish.style.backgroundColor);
console.log("the width:", reddish.style.width);
console.log("from a computed style:", window.getComputedStyle(reddish).backgroundColor);
console.log("from a computed style:", window.getComputedStyle(reddish).width);
}, 100);
#reddish {
background-color: #fa5;
width: 100px;
height: 100px;
}
<html>
<body>
<div id="reddish"></div>
</body>
</html>
【问题讨论】:
标签: javascript css stylesheet getcomputedstyle