【www.bbyears.com--js教程】
iframe 自适应高度,目前最广泛的实现方式是以 JS 获取 iframe 所载入页面的 body 高度。(在本地 html 文件测试的时候 chrome 会有跨域错误,可以使用 IE 来测试)。其实质就是通过 offsetHeight 或者 scrollHeight 获取到 iframe 内部 body 的高度,再调整 iframe 高度即可。以下是初略形式:
Test.html 源码
<script type="text/javascript" charset="utf-8">
function autoHeight(){
var ifr = document.getElementById("auto");
ifr.height = ( ifr.contentDocument && ifr.contentDocument.body.offsetHeight ) ||
( ifr.contentWindow.document.body.scrollHeight );
}
</script>
Test2.html 源码
当然以上只是简单的实现,具体更严谨的计算高度做好使用一些兼容性不错库作为基础。