当前位置: 代码迷 >> Web前端 >> jquery库兑现iframe自适应内容高度和宽度
  详细解决方案

jquery库兑现iframe自适应内容高度和宽度

热度:160   发布时间:2012-07-02 17:46:22.0
jquery库实现iframe自适应内容高度和宽度
javascript原生和jquery库实现iframe自适应内容高度和宽度---推荐使用jQuery的代码!

?<iframe src="index.php" id="mainiframe" name="mainiframe" width="100%"   frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>
基于Jquery库的代码很好实现:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
      $("#mainframe").load(function(){
      $(this).height(0); //用于每次刷新时控制IFRAME高度初始化
      var height = $(this).contents().height() + 10;
      $(this).height( height < 500 ? 500 : height );
    });
});
</script>

?基于JS原生代码的实现:
<script language="javascript">
       if (window.parent.length>0){window.parent.document.all.mainframe.style.height=document.body.scrollHeight;}
</script>
只需在你被iframe调用的文件</body>之后加入上面这段即可!
这个也可以控制iframe的高度随内容的多而自动增长
<iframe name="web" width="100%" frameborder=0 height="100%" src="index.php" id="web" onload="this.height=web.document.body.scrollHeight+20" ></iframe>
  相关解决方案