09-07
18

iframe高度自动调整(通过IE,firefox,opera测试)

1.index.html
    <iframe id="mainIframe" scrolling="no" frameborder="0" width="100%" src="a.html"></iframe>
    <a href="#" onclick="page('a.html')">a页面</a><a href="#" onclick="page('b.html')">b页面</a>

2 a.html  //内嵌页面
    <script language="javascript">
          changeHight();
    </script>

3. b.html  //内嵌页面
     <script language="javascript">
          changeHight();
    </script>

4. page.js
function changeHight(){
  var iFrm = parent.document.getElementById("mainIframe");
     var subWeb = iFrm.contentDocument;
     if(subWeb){
    if (subWeb.body.scrollHeight>480)
   iFrm.height = subWeb.body.scrollHeight+20;
  else
      iFrm.height=500;
  }
  if(top.document.frames["mainIframe"].document && window.document.body.scrollHeight!="0"){
   parent.document.getElementById("mainIframe").style.height=window.document.body.scrollHeight;
  }
  if(top.document.frames["mainIframe"].document && window.document.body.scrollHeight=="0"){
   parent.document.getElementById("mainIframe").style.height=500;
  }
}

function page(page){
    document.getElementById("mainIframe").src=page;
}



造成IE,FireFox,Opera中Iframe显示差异原因在于
1.iframe在FireFox中取法为parent.document.getElementById("mainIframe").contentDocument,而在ie,opera中为parent.document.getElementById("mainIframe").document
2.当页面无滚动时,window.document.body.scrollHeight在IE中能取到,fireFox和Opera中取不到。


[本日志由 blurxx 于 2009-07-18 12:50 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: iframe 自动调整
相关日志:
评论: 0 | 引用: 0 | 查看次数: 258
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭