首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

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

2012-11-15 
IFrame自动适应内容高度,不出现滚动条在页面中想要插入一个IFRAME,但需要这个Iframe随着Iframe里的内容高

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">

热点排行