[重学前端]JS dom.style.xxx获取出来是空 记录一下这个坑

244 阅读1分钟
//css部分
.box{
    width : 100px;
    background-color:red
}
//html部分
<div class="box" style="height:100px">
111111
</div>
//JS部分
console.log(document.querySelector('.box').style.width)
//这是获取不到的,可以使用offsetWidth  offsetXXX这种,还有一种在下面
console.log(document.querySelector('.box').style.height)//写在html里面的行内,这个可以获取
//所以想要获取css里面的东西可以使用getComputedStyle方法
console.log(window.getComputedStyle(dom).属性值)