【转来的】让iframe自动调整高度的方法

转自:http://hi.baidu.com/ssitn/blog/item/c6f1e954ee6e6054d0090664.html

在页面中使用iframe时,对其高度的控制十分头疼,试验了别人介绍的各种方法,终于找到一个比较完美的解决方法。

这个方法来自:JavaScript分享网 http://www.sharejs.com/showdetails-904.aspx

简介:在一个页面里调用另外iframe子页面,可以通过javascript在父页面直接控制子页面的高度,无需在子页面添加任何代码,非常好用。

兼容性:IE6+ FireFox2+ Opera9+ 更新时间:2009-7-31

<script type="text/javascript">
/******************************************************
*
让iframe自动调整高度的方法
Share JavaScript (http://www.ShareJS.com)
* 使用此脚本程序,请保留此声明
* 获取此脚本以及更多的JavaScript程序,请访问 http://www.ShareJS.com
******************************************************/

function SetCwinHeight(){
var sharejs_frame=document.getElementById("sharejs_frame"); //iframe id
if (document.getElementById){
if (sharejs_frame && !window.opera){
if (sharejs_frame.contentDocument && sharejs_frame.contentDocument.body.offsetHeight){
sharejs_frame.height = sharejs_frame.contentDocument.body.offsetHeight;
}else if(sharejs_frame.Document && sharejs_frame.Document.body.scrollHeight){
sharejs_frame.height = sharejs_frame.Document.body.scrollHeight+30;
}
}
}
}
</script>


<iframe width="100%" id="sharejs_frame" οnlοad="SetCwinHeight()" height="0″ frameborder="0″ src="frame.html" scrolling="no"></iframe>


效果的确不错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值