html
<div class="box"></div>
css
.box{
width: 200px;
height: 200px;
background-color: red;
}
js
let box = document.querySelector(".box");
console.log(box.style.width); // 输出未空
问:为什么会获取不到值呢?
答:因为这里的style只会获取内嵌样式,然而我们并没有在标签内写样式,所以获取的就是空;
解决:
console.log(window.getComputedStyle(box).width);