获取HTML元素的宽度:
- offsetWidth(四舍五入,取整):内容宽度 + 左右border宽度 + 左右padding宽度。注:不包含左右margin宽。
- clientWidth(四舍五入,取整):内容宽度 + 左右padding宽度
<div id="box"></div>
const el = document.querySelector('#box')
console.log(el.offsetWidth, el.clientWidth)
扩展:
- 获取某个元素的父节点:document.getElementById("item1").parentNode
参考网址:offsetwidth详解_offsetWidth和它的兄弟姐妹们_weixin_39905500的博客-CSDN博客
clientWidth、offsetWidth、区别_万事胜意sy的博客-CSDN博客_clientwidth offsetwidth