[英]How to get width and height of an element on the page?
借助可爱的jQuery?
jQuery has two methods, width and height, to do this very thing: jQuery有两种方法,即width和height,可以做到这一点:
$(".myElement").width();
$(".myElement").height();
You can also use them to SET values: 您还可以使用它们来设置值:
$(".myElement").width(200); // set element to 200px
Further information can be found in the documentation: http://api.jquery.com/width/ and http://api.jquery.com/height/ 可以在文档中找到更多信息: http : //api.jquery.com/width/和http://api.jquery.com/height/
Note the other dimension methods available too: 请注意其他可用的尺寸标注方法 :
$.innerHeight()
- Get the current computed height for the first element in the set of matched elements, including padding but not border. $.innerHeight()
- 获取匹配元素集合中第一个元素的当前计算高度,包括填充,但不包括边框。 $.innerWidth()
- Get the current computed width for the first element in the set of matched elements, including padding but not border. $.innerWidth()
- 获取匹配元素集中第一个元素的当前计算宽度,包括填充,但不包括边框。 $.outerHeight()
- Get the current computed height for the first element in the set of matched elements, including padding and border. $.outerHeight()
- 获取匹配元素集中第一个元素的当前计算高度,包括填充和边框。 $.outerWidth()
- Get the current computed width for the first element in the set of matched elements, including padding and border. $.outerWidth()
- 获取匹配元素集合中第一个元素的当前计算宽度,包括填充和边框。 Using the height(...)
and width(...)
methods: 使用
height(...)
和width(...)
方法:
alert($(body).height());
alert($(body).width());
You'll never guess... 你永远不会猜到...
var width = $(selector).width();
var height = $(selector).height();
In various useful cases, the width value is often wrong on Mac Firefox/Mozilla . 在各种有用的情况下, Mac Firefox / Mozilla上的width值通常是错误的 。
-- where wrong means that if you set an enclosing div to the reported "width" of the text-child, you'll get wrapping. -错误的意思是,如果您将封闭的div设置为所报告的子文本的“宽度”,则会得到换行。
The problem is that on Mac Firefox/Mozilla, Firefox uses sub-pixel font rendering. 问题在于,在Mac Firefox / Mozilla上,Firefox使用亚像素字体渲染。 And the width that is returned is the floor, not the ceiling of the true width, which is a float.
返回的宽度是地板,而不是真正宽度的天花板,即浮动。
My bug report gives a work-around for Firefox, or just always add 1px to the width and you'll be ok. 我的错误报告提供了针对Firefox的解决方法,或者总是将宽度添加1px,这样您就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.