Comparison between native js methods for obtaining width and height and jquery methods for obtaining width and height

Source: Internet
Author: User

Note: 1. Because the obtained height is the same as the obtained width, the following only describes how to obtain the width.
2. The values returned by all the methods and attributes described below are not in units.
3. For convenience, the following conditions are abbreviated:

Obj-> represents a DOM object in native JS; represents a JQuery object in JQuery
Width-> obj. style. width
OffsetWidth-> obj. offsetWidth
$ Width-> obj. width ()
$ InnerWidth-> obj. innerWidth ()
$ OuterWidth-> obj. outerWidth ()
Padding-> indicates the sum of padding-left and padding-right of the object.
Border-> indicates the sum of border-left-width and border-right-width of the object.

Attributes related to width obtained by native JS include width and offsetWidth. The method for obtaining width is obj. style. width, which can be obtained only when the object is Set width through embedding. Otherwise, an empty string is returned. The value obtained by offsetWidth is the same as the outerWidth of the object obtained by JQuery. offsetWidth is not a standard attribute but is well supported.

JQuery has three methods to obtain the width: width (), innerWidth (), and outerWidth. Usage: obj. width (), obj. innerWidth (), obj. outerWidth (). Width () is the content width of the object, innerWidth () is the sum of the content width and fill width of the object, outerWidth () the obtained width includes the border, fill width, and content width.

The relationships between these five methods are as follows:

Width = $ width;
OffsetWidth = border + padding + width;
$ InnerWidth = padding + width;
$ OuterWidth = border + padding + width;

These five methods are compatible with firefox, chrome, opera, safari, ie6, ie7, ie8, and ie9, but there are two problems: 1. If width is not set, when chrome opened the page for the first time, all the obtained widths were incorrect. The result is the same as that in firefox for the second time. 2. If ie6 is not configured with a fixed width, no scroll bars will appear.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.