您的位置:首页 > Web前端 > JavaScript

js判断div高度及设置父窗口iframe高度

2017-01-19 14:07 337 查看
Javascript如何获取和设置div的高度和宽度,并且兼容任何浏览器?看代码:

<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高度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript div iframe
相关文章推荐