当内部为素为绝对定位时,不能把外部元素撑开,我们可以用给父元素设定最小高度的方法来解决这个问题。解决方法如下。
<div style=”position:relative” id=”a1″>
<div style=”position:absolute” id=”a2″>这是内容</div>
</div>
window.onload = function () { var height = getHeight(); var a3 = document.getElementById("a1"); a3.style.minHeight = height + 20 + "px"; console.log(height); } $(window).resize(function () { var height = getHeight(); var a3 = document.getElementById("a1"); a3.style.minHeight = height + 20 + "px"; }); function getHeight() { var height = 0; var div = document.getElementById("a2").getBoundingClientRect(); if (div.height) { height = div.height; } else { height = div.bottom - div.top; } return height; }