js 获取div高度

2024-02-15 17:14:48 举报文章

js获取div元素的高度与宽度要用的 clientHeight 与 clientWidth方法

clientHeight:返回元素的可视高度

clientWidth:返回元素的可视宽度

<div id="mochu" style="height: 200px;width:150px;color:#fff;background-color: blueviolet;">
    https://www.qzphp.cn
</div>
<script>
    //获取高度
    var h = document.getElementById('mochu').clientHeight;
    //获取宽度
    var w = document.getElementById('mochu').clientWidth;
    console.log(h);
    console.log(w);
</script>

jq获取高度

<div id="mochu" style="height: 100px;width:150px;">
    https://www.qzphp.cn
</div>
<script>
    //获取高度
    var h = $('#mochu').height();
    //获取宽度
    var w = $('#mochu').width();
    console.log(h);
    console.log(w);
</script>
如果你认为本文可读性较差,内容错误,或者文章排版错乱,请点击举报文章按钮,我们会立即处理!