天天看點

iframe 自适應高度,多層嵌套iframe自适應高度

一、單個iframe 高度自适應

<iframe src="yx/yx_list.html" width="100%" [color=red]onload[/color]="[color=blue]this.height=this.contentWindow.document.body.scrollHeight[/color]" scrolling="no" name="rtFrame" frameborder="0"></iframe>

目前視窗自适應高度:this.height=this.contentWindow.document.body.scrollHeight

子窗體視窗自适應高度:

window.parent.document.getElementById("rtFrame").height=document.body.scrollHeight;

二、多層嵌套iframe 高度自适應

A頁面的iframe 加載B.html檔案:

<iframe id="frame_content" src=”B.html“ name="right" width="1003" frameborder="0" scrolling="no" ></iframe>

B.html頁面也有一個iframe:

<iframe width="750" name="rightform" id="rightform" src="KinTimMng_right_init.php" frameborder="0" scrolling="no" [color=blue]οnlοad="this.height=this.contentWindow.document.body.scrollHeight;parent.document.getElementById('frame_content').style.height= document.body.scrollHeight + 'px';"[/color] ></iframe>

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'

繼續閱讀