当前位置: 代码迷 >> Web前端 >> IFrame自动顺应内容高度,不出现滚动条
  详细解决方案

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

热度:32   发布时间:2012-11-15 15:16:13.0
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">
  相关解决方案