javascript獲取設(shè)置div的高度和寬度兼容任何瀏覽器
更新時(shí)間:2013年09月22日 16:36:04 作者:
Javascript如何獲取和設(shè)置div的高度和寬度,并且兼容任何瀏覽器,感興趣的朋友不妨看看下面的代碼或許有意想不到的收獲
Javascript如何獲取和設(shè)置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等
設(shè)置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等
復(fù)制代碼 代碼如下:
<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的寬高度:
復(fù)制代碼 代碼如下:
alert(document.getElementById("div1").offsetHeight); //兼容FF、IE等
alert(document.getElementById("div1").offsetWidth); //兼容FF、IE等
設(shè)置div1的寬高度為div2的寬高度:
復(fù)制代碼 代碼如下:
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等
相關(guān)文章
JavaScript使用類似break機(jī)制中斷forEach循環(huán)的方法
這篇文章主要介紹了JavaScript使用類似break機(jī)制中斷forEach循環(huán)的方法,需要的朋友可以參考下2018-11-11
js中substr,substring,indexOf,lastIndexOf的用法小結(jié)
本篇文章主要是對(duì)js中substr,substring,indexOf,lastIndexOf的用法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
分享javascript計(jì)算時(shí)間差的示例代碼
這篇文章主要為大家介紹了javascript計(jì)算時(shí)間差的示例代碼,,一般來說都是計(jì)算當(dāng)前時(shí)間和一個(gè)指定時(shí)間點(diǎn)之間的差距,感興趣的小伙伴們可以參考一下2016-01-01
javascript jquery對(duì)form元素的常見操作詳解
下面小編就為大家?guī)硪黄猨avascript jquery對(duì)form元素的常見操作詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
Three.js中網(wǎng)格對(duì)象MESH的屬性與方法詳解
三維開發(fā)渲染最多的對(duì)象大概是網(wǎng)格mesh了,Webgl開發(fā)三維也不例外,下面這篇文章主要給大家介紹了關(guān)于Three.js中網(wǎng)格對(duì)象MESH的屬性與方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
javascript實(shí)現(xiàn)掃雷簡(jiǎn)易版
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)掃雷簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08

