1. 程式人生 > >js判斷div高度及設定父視窗iframe高度

js判斷div高度及設定父視窗iframe高度

<div id="div1" style="height:300px;width:200px;">http://www.itdos.com</div>

<div id="div2" style="height:30px;width:20px;">http://www.itdos.com</div>

獲取div1的寬高度:

alert(document.getElementById("div1").offsetHeight);  //相容FF、IE等

alert(document.getElementById("div1").offsetWidth);    //相容FF、IE等

設定div1的寬高度為div2的寬高度:

document.getElementById("div1").style.height=document.getElementById("div2").offsetHeight;      //僅IE

document.getElementById("div1").style.height=document.getElementById("div2").offsetHeight+ "px";      //相容FF、IE等

document.getElementById("div1").style.width=document.getElementById("div2").offsetWidth;//僅IE

document.getElementById("div1").style.width=document.getElementById("div2").offsetWidth+ "px";//相容FF、IE等

以下為獲取設定父視窗iframe高度

var parentFrame = parent.document.getElementsByTagName("IFRAME");//獲取父視窗的iframe

var h = document.documentElement.scrollHeight;//獲取子視窗的高度 FF
h = document.body.scrollHeight; //IE

parentFrame

.style.height = h +"px";//設定父視窗iframe高度