iframe实现自适应高度
代码简单,兼容性还可以
<script>
function SetWinHeight(obj)
{
var win=obj;
if (document.getElementById)
{
if (win && !window.opera)
{
if (win.contentDocument && win.contentDocument.body.offsetHeight)
win.height = win.contentDocument.body.offsetHeight;
else if(win.Document && win.Document.body.scrollHeight)
win.height = win.Document.body.scrollHeight;
}
}
}
</script>
最后,加入iframe,不能丢掉onload属性,当然了,id也必须也函数中的win匹配
<iframe height="200" id="win" name="win" onload="Javascript:SetWinHeight(this)" frameborder="0" scrolling="no" src="1.htm"></iframe>
本文介绍了一种简单且兼容性良好的方法来设置iframe的高度,通过使用JavaScript函数来获取子页面的实际高度,确保iframe内容能自适应地填充其容器。此技巧适用于网页布局中灵活展示不同页面内容的需求。
268

被折叠的 条评论
为什么被折叠?



