javascript獲取設(shè)置div的高度和寬度兼容任何瀏覽器
更新時間: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)文章
用js腳本控制asp.net下treeview的NodeCheck的實現(xiàn)代碼
根據(jù)TreeView2.js修改后的TreeView父節(jié)點與子節(jié)點的CheckBox聯(lián)動.2010-03-03JavaScript動畫原理之如何使用js進(jìn)行動畫效果的實現(xiàn)
在現(xiàn)在做頁面很多時候都會用上動畫效果,比如下拉菜單,側(cè)邊搜索欄,層的彈出與關(guān)閉等等,下面這篇文章主要給大家介紹了關(guān)于JavaScript動畫原理之如何使用js進(jìn)行動畫效果實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-04-04javascript 構(gòu)造函數(shù)強制調(diào)用經(jīng)驗總結(jié)
本文將介紹javascript構(gòu)造函數(shù)調(diào)用方面的案例應(yīng)用,需要了解的朋友可以參考下2012-12-12微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法,涉及微信小程序swiper選項卡組件相關(guān)操作技巧,需要的朋友可以參考下2018-12-12