IFrame自动适应内容高度,不出现滚动条

本文介绍了一种使IFRAME能根据其内容自动调整高度的方法。通过使用JavaScript函数SetCwinHeight,可以确保IFRAME的高度始终与内部文档的高度保持一致。
在页面中想要插入一个IFRAME,但需要这个Iframe随着Iframe里的内容高度自动调整高度,这个时候方法有很多种,在这里只给大家介绍其中一种方法,也是比较实用方便的方法。
代码:
将下面JS代码放在<head></head>之间:

<script type="text/javascript">
function SetCwinHeight(iframeObj){
if (document.getElementById){
if (iframeObj && !window.opera){
if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight){
iframeObj.height = iframeObj.contentDocument.body.offsetHeight;
}else if(document.frames[iframeObj.name].document && document.frames[iframeObj.name].document.body.scrollHeight){
iframeObj.height = document.frames[iframeObj.name].document.body.scrollHeight;
}
}
}
}
</script>

iFrame示例代码为:
<iframe frameborder="0" scrolling="no" name="right" onload="SetCwinHeight(this)" src="页面" width="800">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值