js获取元素的宽高的方法

HTML代码:
<section class="sec"  style="width: 400px;">
<style type="text/css">
	.sec {
		background-color: red;
		overflow: hidden;
		/*width: 500px;*/
	}
	.child {
		background-color: yellow;
		height: 100px;
		margin-top: 20px;
	}
</style>
	<article class="child"></article>	
</section>

js代码:

var dom = document.getElementsByClassName('sec')[0];
	var w1 = dom.style.width;	//此api只能获取到内联样式的属性值
	var w2 = dom.currentStyle.width;  //此api虽然支持全部三种设置样式的方式,但是只支持IE
	var w3 = window.getComputedStyle(dom).width;	//此api支持IE、Chrome、Firefox的全部三种样式
	var w4 = dom.getBoundingClientRect().width; //同样能获取及时的尺寸,支持IE、Chrome、Firefox,只是获取到的是数值不带单位
	dom.onclick = function () {
		 alert(w4)
	}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值