javascript获取设置div的高度和宽度兼容任何浏览器
更新时间:2013年09月22日 16:36:04 作者:
Javascript如何获取和设置div的高度和宽度,并且兼容任何浏览器,感兴趣的朋友不妨看看下面的代码或许有意想不到的收获
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等
复制代码 代码如下:
<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等
相关文章
JavaScript使用类似break机制中断forEach循环的方法
这篇文章主要介绍了JavaScript使用类似break机制中断forEach循环的方法,需要的朋友可以参考下2018-11-11
js中substr,substring,indexOf,lastIndexOf的用法小结
本篇文章主要是对js中substr,substring,indexOf,lastIndexOf的用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助2013-12-12
javascript jquery对form元素的常见操作详解
下面小编就为大家带来一篇javascript jquery对form元素的常见操作详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-06-06


最新评论