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循環(huán)的方法
這篇文章主要介紹了JavaScript使用類似break機制中斷forEach循環(huán)的方法,需要的朋友可以參考下2018-11-11
js中substr,substring,indexOf,lastIndexOf的用法小結
本篇文章主要是對js中substr,substring,indexOf,lastIndexOf的用法進行了總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
javascript jquery對form元素的常見操作詳解
下面小編就為大家?guī)硪黄猨avascript jquery對form元素的常見操作詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
Three.js中網(wǎng)格對象MESH的屬性與方法詳解
三維開發(fā)渲染最多的對象大概是網(wǎng)格mesh了,Webgl開發(fā)三維也不例外,下面這篇文章主要給大家介紹了關于Three.js中網(wǎng)格對象MESH的屬性與方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09

