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彈出一個(gè)層并增加一個(gè)覆蓋層
彈出一個(gè)層的js代碼,比較不錯(cuò)2008-10-10用js腳本控制asp.net下treeview的NodeCheck的實(shí)現(xiàn)代碼
根據(jù)TreeView2.js修改后的TreeView父節(jié)點(diǎn)與子節(jié)點(diǎn)的CheckBox聯(lián)動(dòng).2010-03-03JavaScript動(dòng)畫(huà)原理之如何使用js進(jìn)行動(dòng)畫(huà)效果的實(shí)現(xiàn)
在現(xiàn)在做頁(yè)面很多時(shí)候都會(huì)用上動(dòng)畫(huà)效果,比如下拉菜單,側(cè)邊搜索欄,層的彈出與關(guān)閉等等,下面這篇文章主要給大家介紹了關(guān)于JavaScript動(dòng)畫(huà)原理之如何使用js進(jìn)行動(dòng)畫(huà)效果實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-04-04js實(shí)現(xiàn)從左向右滑動(dòng)式輪播圖效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)從左向右滑動(dòng)式輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07javascript 構(gòu)造函數(shù)強(qiáng)制調(diào)用經(jīng)驗(yàn)總結(jié)
本文將介紹javascript構(gòu)造函數(shù)調(diào)用方面的案例應(yīng)用,需要了解的朋友可以參考下2012-12-12微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動(dòng)條不顯示的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動(dòng)條不顯示的方法,涉及微信小程序swiper選項(xiàng)卡組件相關(guān)操作技巧,需要的朋友可以參考下2018-12-12