![](/img/trans.png)
[英]How to get min height/width of parent element and set to it's child height/width by CSS
[英]How to get width and height of an element on the page?
借助可愛的jQuery?
jQuery有兩種方法,即width和height,可以做到這一點:
$(".myElement").width();
$(".myElement").height();
您還可以使用它們來設置值:
$(".myElement").width(200); // set element to 200px
可以在文檔中找到更多信息: http : //api.jquery.com/width/和http://api.jquery.com/height/
$.innerHeight()
- 獲取匹配元素集合中第一個元素的當前計算高度,包括填充,但不包括邊框。 $.innerWidth()
- 獲取匹配元素集中第一個元素的當前計算寬度,包括填充,但不包括邊框。 $.outerHeight()
- 獲取匹配元素集中第一個元素的當前計算高度,包括填充和邊框。 $.outerWidth()
- 獲取匹配元素集合中第一個元素的當前計算寬度,包括填充和邊框。 使用height(...)
和width(...)
方法:
alert($(body).height());
alert($(body).width());
你永遠不會猜到...
var width = $(selector).width();
var height = $(selector).height();
在各種有用的情況下, Mac Firefox / Mozilla上的width值通常是錯誤的 。
-錯誤的意思是,如果您將封閉的div設置為所報告的子文本的“寬度”,則會得到換行。
問題在於,在Mac Firefox / Mozilla上,Firefox使用亞像素字體渲染。 返回的寬度是地板,而不是真正寬度的天花板,即浮動。
我的錯誤報告提供了針對Firefox的解決方法,或者總是將寬度添加1px,這樣您就可以了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.