Get element width in px

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

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

HTML

         


        
6条回答
  •  萌比男神i
    2020-12-10 13:19

    How many items have the class myElement? Consider using an id, not a class, as getting the width of two elements is not really possible (or logically understandable IMO).

    I made a little demo, and for me, it outputs the width in pixels for a single span element with a width of 100% (for me, it alerts something around 400): http://jsfiddle.net/LqpNK/3/.

    By the way, element's can't have a set width or height, so setting their width and height does you no good. Instead, display them as a block element (so just replace with

    , or add display: block; to the CSS of .myElement).

提交回复
热议问题