javascript获取设置div的高度和宽度兼容任何浏览器

时间:2013-09-22    点击:89   
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等
通过AJAX的JS、JQuery两种方式解析XML示例介绍
js如何取消事件冒泡
页面载入结束自动调用js函数示例
往光标所在位置插入值的js代码
jquery设置元素的readonly和disabled的写法
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved