简体   繁体   中英

.height() returns height in percent, how to get it in pixels

I've got an element that I want to determine its height and width so I can fill it properly, but when I use

$('#element').height();
$('#element').width();

it returns the values in percentages. I've been looking for a solution, but all the questions that come up in my searches imply that jQuery returns the pixels by default, and so do not explain how to get it to do so.

I believe you must be mistaken. Can you provide a demonstration where jQuery is returning a percent value for .width or .height?

What exactly is being returned? You may be seeing a fractional value (.75) instead of an integer. From the .height() documentation :

The number returned by dimensions-related APIs, including .height(), may be fractional in some cases. Code should not assume it is an integer. Also, dimensions may be incorrect when the page is zoomed by the user; browsers do not expose an API to detect this condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM