javascript - How to get the exact width of an inline element that has no width set?
PHP中文网
PHP中文网 2017-05-19 10:46:37
0
3
900

For example:

td has the width set, span has no width set.

1. Use $.width() to get the integer width, $('td').width() to get the 218px, span is 129px
2. Use clientWidth to get the width without margins and boder , span width is 0, td width is '218'
3. Use getComputedStyle to get final width , accurate to the decimal point , but the width of span obtained is auto, td is 217.5

question:

How to get the exact width of an element with no width set (accurate to the decimal point, displayed in px units)?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
Peter_Zhu

Try using getClientBoundingRect()

迷茫
getComputedStyle('原生dom')['width']
迷茫
<p id="p" style="width:88.88px;"></p>
parseFloat($("#p").attr("style").split('width:')[1])
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template