【发布时间】:2016-12-07 20:07:04
【问题描述】:
我有以下html结构:
<div id="container">
<h1>This is an h1 element</h1>
</div>
当我尝试使用 javascript 在 firefox 或 chrome div 中查找容器的高度时:
var container = document.getElementById("container");
var offsetHeight = container.offsetHeight;
我得到了错误的 offsetHeight(如果与 clientHeight 一起使用)。 我只得到 H1 本身的高度,而不是元素周围的前边距/后边距。
有没有办法得到真实的高度?
【问题讨论】:
-
如果你不能使用 jQuery,我也在我的答案中添加了纯 JS 解决方案
标签: javascript html height css offsetheight