Home >Web Front-end >JS Tutorial >javascript获取设置div的高度和宽度兼容任何浏览器_javascript技巧

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

WBOY
WBOYOriginal
2016-05-16 17:21:411431browse

Javascript如何获取和设置div的高度和宽度,并且兼容任何浏览器?看代码:

复制代码 代码如下:

http://www.itdos.com

http://www.itdos.com


获取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等
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn