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.