JS实现iframe框架自适应高度

1、DIV盒子里嵌入 iframe框架,调用gg.html页面
 
<div align="center">
<iframe id="main" style="width:100%;" name="main" src=" gg.html" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" οnlοad="Javascript:setFrameHeight(this)"></iframe>
//方法一(经测试发现,方法一有时不起作用,建议用方法二)
<script language="javascript">
<!--
function setFrameHeight(obj){
 var win=obj;
 if (document.getElementById)
 {
  if (win  &&  !window.opera)
  {
   if (win.contentDocument  &&  win.contentDocument.body.offsetHeight)

    win.height = win.contentDocument.body.offsetHeight+"px";
   else if(win.Document  &&  win.Document.body.scrollHeight)
    win.height = win.Document.body.scrollHeight+"px";
  }
 }
}
-->  
</script>
//方法二
<script type="text/javascript"> 
//注意:下面的代码是放在和iframe同一个页面中调用
$("#mainFrame").load(function () {
    var mainheight = $(this).contents().find("body").height() + 30;
    $(this).height(mainheight);
});  
</script> 
</div>

2、gg.html页面的代码: 
<a href="#" target="_blank"><img src="gg/pic1.gif" width="100%" height="60" style="margin-left: 0px;margin-top: 10px"/></a>
<a href="#" target="_blank"><img src="gg/ pic2 .gif" width="100%" height="60" style="margin-left: 0px;margin-top: 10px"/></a>
<a href="#" target="_blank"><img src="gg/ pic3 .gif " width="100%" height="60" style="margin-left: 0px;margin-top: 10px"/></a>
<a href="#" target="_blank"><img src="gg/ pic4 .gif " width="100%" height="60" style="margin-left: 0px;margin-top: 10px"/></a>
<a href="#" target="_blank"><img src="gg/ pic5 .gif " width="100%" height="60" style="margin-left: 0px;margin-top: 10px"/></a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值