Get element width in px

后端 未结 6 1540
-上瘾入骨i
-上瘾入骨i 2020-12-10 13:06

How to get element width in pixels (px)? jQuery always returns value in percent (pct).

HTML

         


        
6条回答
  •  轮回少年
    2020-12-10 13:34

    From the jQuery documentation on width()

    The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

    So it returns pixels if the pixel width is defined, but you are defining the width in %

提交回复
热议问题