dom 结构
<div id="id1" class="class1" ref="ref1" style="width: 100px; height: 100px;"></div>
通过 id 获取高度宽度
let widthid1 = document.getElementById("id1").offsetWidth
let heightid = document.getElementById("id1").offsetHeight
通过class 获取高度宽度
let widthclass1 = document.getElementsByClassName("class1").offsetWidth
let heightclass1 = document.getElementsByClassName("class1").offsetHeight
通过ref 获取高度宽度
let widthref1 = this.$refs.ref1.offsetWidth
let heightref1 = this.$refs.ref1.offsetHeight

本文介绍了如何使用JavaScript通过ID、Class及Ref获取DOM元素的高度和宽度。分别展示了使用getElementById和getElementsByClassName方法来获取指定ID或Class的元素尺寸,并且提到了在现代框架中利用Ref来直接访问DOM节点。
1798





