<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获取和设置HTML元素(如div)的高度和宽度的方法,该方法能够兼容主流浏览器,包括IE和Firefox。通过简单的示例代码展示了如何实现这一功能。
1201

被折叠的 条评论
为什么被折叠?



