源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript获取设置div的高度和宽度兼容任何浏览器

  • 时间:2022-03-30 00:20 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript获取设置div的高度和宽度兼容任何浏览器
Javascript如何获取和设置div的高度和宽度,并且兼容任何浏览器?看代码:
[u]复制代码[/u] 代码如下:
<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的宽高度:
[u]复制代码[/u] 代码如下:
alert(document.getElementById("div1").offsetHeight); //兼容FF、IE等 alert(document.getElementById("div1").offsetWidth); //兼容FF、IE等
设置div1的宽高度为div2的宽高度:
[u]复制代码[/u] 代码如下:
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等
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部