Javascript如何獲取和設置div的高度和寬度,并且兼容任何瀏覽器?看代碼:
復制代碼 代碼如下:
<div></div>
<div></div>
復制代碼 代碼如下:
alert(document.getElementById("div1").offsetHeight); //兼容FF、IE等
alert(document.getElementById("div1").offsetWidth); //兼容FF、IE等
復制代碼 代碼如下:
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等
新聞熱點
疑難解答
圖片精選