除非我用css

时间:2017-07-22 23:11:12

标签: javascript angular

如何在CSS中使用设置宽度属性来获取元素宽度?如果我设置它的宽度,但是如果我不宽度只是0,即使我看到它不是在用调试器检查之后。

let width = this.htmlElement.clientWidth;

HTML

<label [model]="foobar">0</label>

1 个答案:

答案 0 :(得分:1)

使用offsetWidth代替

let width = this.htmlElement.offsetWidth;

clientWidth Element.clientWidth中的第一行是

  

对于没有CSS 或的元素,var el = document.getElementById('foobar'); console.log('clientWidth : '+ el.clientWidth ); console.log('offsetWidth : '+ el.offsetWidth );属性为零   内联布局框,否则它是元素的内部宽度   像素

另一方面,offsetWidth是一个只读属性,它返回元素的布局宽度,无论元素是否具有给定宽度的样式。

<label id="foobar">This has width ..................</label>
uint8_t