我的网页index.html里有一个iframe,通过以下代码实现了高度自适应,
<iframe name="ifrm" id="ifrm" src="main.html" width="100%" onload="this.height=ifrm.document.body.scrollHeight" scrolling="no" align="middle" frameborder="0"> </iframe>然后我在main.html文件里有一个tab表格,通过以下代码控制显示的内容<script> 
<!-- 
function setTab(name,cursel,n){ 
for(i=1;i<=n;i++){ 
var menu=document.getElementById(name+i); 
var con=document.getElementById("con_"+name+"_"+i); 
menu.className=i==cursel?"hover":""; 
con.style.display=i==cursel?"block":"none"; 
  }
var newHeight = document.body.scrollHeight + 20 + "px";       
window.parent.document.getElementById("ifrm").style.height = newHeight;

//--> 
其中加粗的两行是用来实现当点击一个tab时,iframe高度自适应,这一部分也成功了但是问题来了: 当使用了setTab 函数自适应高度后,再点击index.html里的链接切换iframe内容时,自适应高度就失效了,始终保持tab的高度,不能切换到其他网页的高度了。这是为什么?怎么解决?出现问题的网址是http://www.youbeiou.com/index_test.html

解决方案 »

  1.   

    你里面还有一层frame吧...
    最外层的应该是没问题的...
      

  2.   

    iframe自行适应高度例子:
     <table id="mainwidth">
           <tr> 
              <td id="mainview" >
              <iframe src=" filmByChannel.jsp?channelindex=<%=request.getParameter("channelindex")%>" width="100%" height="100%" name="main" scrolling="no" border="0" frameborder="0">
              这里是内框架,如果看不到请升级你的IE。</iframe></td>
           </tr>
    </table>
    <script language="JavaScript" for="main" event="onload"> <!--本页面加载时动态改变iframe高度-->
    mainview.height=main.document.body.scrollHeight+10;
    mainwidth.width=main.document.body.scrollWidth+10;
    //mainview.width=main.document.body.scrollWidth+10;
    </script>
      

  3.   

    是不是在iframe中没有添加position = 'absolute';的问题。
      

  4.   

    谢谢各位的回复澄清一下我的问题:
    内部没有框架,就一个框架,只是iframe内部的页面里有一个javascript控制的tab表格
    可以到我提供的地址上去看看就清楚了。http://www.youbeiou.com/index_test.html
    1楼的同志: 什么类型转换问题?能说具体点么?谢谢
    5楼的同志: position = 'absolute';在哪里添加,我在iframe所在的div测试了一下,不行
    这个问题很妖怪啊,我DOM基础不行,还麻烦各位高手帮我解决一下,谢了先
      

  5.   

    这里好明显有两个IFRAME了...你看那个目的地介绍的内层代码...
    <div id="Layer1">
      <div id="left_navigator">
        <p class="listclass"><a href="Destination_main.html" target="destination_frame">目的地列表:</a></p>
        <p class="listclass"><a href="destination/stockholm.html" target="destination_frame">- 斯德哥尔摩</a></p>
        <p class="listclass"><a href="destination/helsinki.html" target="destination_frame">- 赫尔辛基</a>  </p>
        <p class="listclass"><a href="destination/gotland.html" target="destination_frame">- 哥特兰岛</a></p>
        <p class="listclass"><a href="destination/turku.html" target="destination_frame">- 图尔库</a>  </p>
        <p class="listclass"><a href="destination/tallin.html" target="destination_frame">- 塔林</a></p>
        <p class="listclass"><a href="destination/riga.html" target="destination_frame">- 里加</a> </p>
        <p class="listclass"></p>
        <p></p>
      </div>
      <div id="right_frame">
        <iframe name="destination_frame" id="destination_frame" src="Destination_main.html" height="410px" width="100%" scrolling="yes" align="left" frameborder="0"> </iframe>
      </div>
      
    </div>
      

  6.   

    回复 常乐:谢谢你的回复。目的地介绍的页面确实是有内层嵌套的iframe。但是首页只有一个iframe,也出现同样的问题。(点击完tab后,再点击“首页”,首页也不能自适应高度了)所以我觉得问题不是出在内层嵌套。欢迎高手指点
      

  7.   

    所有TAB和首页之间都测试过.是正常的
    因为onload是在页面完全loaded的时候触发的.
    如果网速太慢就会有延误的情况.
    又或者你某个JS出错也会导致自动调节终止.
      

  8.   

    window.parent.document.getElementById("ifrm").style.height = newHeight;
    改成window.parent.document.getElementById("ifrm").height = newHeight;
    试试不加style
      

  9.   

    测试了一下11楼的方法,成功!!散分请问cww2010, 加style和不加的区别,从原理上讲,是什么?另外,这种自适应方法在FF中好像不起效,不知是何原因,需要怎么改进兼容性?