多层嵌套iframe 自适应高度的解决办法

多层嵌套iframe 自适应高度的解决方法
下面的是别人写的:

我在页面main.htm中包含框架
<iframe id="main" name="main" src='t1.htm'></iframe>

其中,通过main.htm导航条,动态调整main iframe的内容,实现功能导航。


t1.htm中也包含框架<iframe id="t1" name="t1" src="t2.htm"></iframe>

其中,t1.htm主要为某个功能的操作按钮,通过某些操作生成结果在t1 iframe中显示。


t2.htm中是高度很大的页面,不含其他的框架。

我想要在t1 iframe文挡内容发生变化的时候。能够动态改变t1.htm页面的高度。

同时也要刷新main.htm的main iframe的高度,使得所有的高度进行匹配。

网络上提供的解决方法,都是针对某个iframe 调整的,并不能满足我的需求。
自己摸索了一段时间,才得到解决,虽然解决方法任便不够优美,但可以拿出来。
大家一快学习。其实,原理都不难,难的是Javascript标准的支持不一致,而且调试
很不方便造成的。期间有一本书<<Javascript bible>>(<javascript圣经>),其中的
某些章节对我帮助很大,希望你也可以得到很多收获。
我的解决方法:
在t2.htm中(最好在<head></head>标签内)加入:
<script type="text/javascript">
function window.onload(){
  //parent.document.getElementByIdx("t1").height=document.body.scrollHeight;
  //没有style也可以
parent.document.getElementByIdx("t1").style.height=document.body.scrollHeight;
parent.document.getElementByIdx("t1").style.width=document.body.scrollWidth;

parent.parent.document.getElementByIdx("main").style.height=parent.document.body.scrollHeight;
parent.parent.document.getElementByIdx("main").style.width=parent.document.body.scrollWidth;
}
在t1.htm中(最好在<head></head>标签内)加入:
<script type="text/javascript">
function window.onload(){

  //parent.document.getElementByIdx("main").height=document.body.scrollHeight;
  //没有style也可以
parent.document.getElementByIdx("main").style.height=document.body.scrollHeight;
parent.document.getElementByIdx("main").style.width=document.body.scrollWidth;
}
</script>
这样在t2.htm被载入的时候,动态调整t1 iframe及 main iframe的高度。
在t1.htm被载入的时候,动态调整main iframe的高度











iframe自适应高度,多层嵌套iframe自适应高度的解决方法

在页面无刷新更新方面,虽然现在的ajax很强悍,但是处理代码相对多点。想比之下,iframe就简单多了!处理iframe的自适应宽、高,会经常用到,网上整理了一份,写在这里备用:
单个iframe 高度自适应:
<iframe id="iFrame1" name="iFrame1" width="100%" onload="this.height=iFrame1.document.body.scrollHeight" frameborder="0" src="index.htm"></iframe>
起作用的是这句:onload="this.height=iFrame1.document.body.scrollHeight"
多层嵌套iframe 高度自适应:
A页面的iframe:
<iframe id="frame_content" src=”B.php“ name="right" width="1003" frameborder="0" scrolling="no" ></iframe>
B.php页面又有一个iframe:
<iframe width="750" name="rightform" id="rightform" src="KinTimMng_right_init.php" frameborder="0" scrolling="no" onload="this.height=rightform.document.body.scrollHeight;parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px';" ></iframe>
起作用的代码是这句:onload="this.height=rightform.document.body.scrollHeight;parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px';"
onload的时候执行了两条js语句:
1、设置当前iframe自己的高度自适应
this.height=rightform.document.body.scrollHeight 
2、设置父iframe的高度自适应(注意后面的高度单位px,如果不加单位,firefox下不起作用)
parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px'
以上代码在ie6、ie7、ie8、firefox3.5下测试通过