iframe自適應高度,多層嵌套iframe自適應高度的解決方法

在頁面無刷新更新方面,雖然如今的ajax很強悍,可是處理代碼相對多點。想比之下,iframe就簡單多了!處理iframe的自適應寬、高,會常常用到,網上整理了一份,寫在這裏備用:php

單個iframe 高度自適應:ajax

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

多層嵌套iframe 高度自適應:
A頁面的iframe:
<iframe id="frame_content" src=」B.php「 name="right" width="1003" frameborder="0" scrolling="no" ></iframe>firefox

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

起做用的代碼是這句:onload="this.height=rightform.document.body.scrollHeight;parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px';"htm

onload的時候執行了兩條js語句:get

一、設置當前iframe本身的高度自適應
this.height=rightform.document.body.scrollHeight  iframe

二、設置父iframe的高度自適應(注意後面的高度單位px,若是不加單位,firefox下不起做用)
parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px'it

以上代碼在ie六、ie七、ie八、firefox3.5下測試經過

相關文章
相關標籤/搜索