简体   繁体   English

如何获取页面上元素的宽度和高度?

[英]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.

相关问题 如何获取父元素的最小高度/宽度并通过 CSS 设置为子元素的高度/宽度 - How to get min height/width of parent element and set to it's child height/width by CSS 加载后其元素的宽度/高度最初由PERCENTAGE设置的元素如何获得其实际宽度/高度? - How to get actual width/height of some element after loading whose width/height originally set by PERCENTAGE? 如何获取未设置宽度和高度的img元素的宽度和高度? - How to get the width and height of an img element where width and height have not been set? 如何在YUI3中获得计算的元素宽度和高度? - How to get calculated element width and height in YUI3? 我如何获得模态中的元素宽度/高度(bootstrap) - How can i get element width / height in modal ( bootstrap ) 如何使用 jquery 获取 div 元素的宽度和高度? - How can I get the width and height of a div element with jquery? 根据元素的高度和宽度在页面上定位 - Position element on page based on its height and width Javascript iPad,iPhone和Android-如何获取页面的完整宽度和高度? - Javascript iPad, iPhone and Android - How to get page full width and height? 如何获取页面的可视部分的宽度和高度? - How to get page's visual section's width and height? 转换后获取元素的高度和宽度 - Get height and width of a element after trasforming
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM