Implementation Code for Retrieving Element width heights in various scenarios

Source: Internet
Author: User

Scenario 1: The width/height attribute of the element style is set Copy codeThe Code is as follows: <div style = "width: 100px;"> test <div>
<Script>
Var div = document. getElementsByTagName ('div ') [0];
Alert (div. style. width );
</Script>

As shown above, use el. style. width. If the width is not set in the style attribute, you cannot use el. style. width, as shown in the following figure.Copy codeThe Code is as follows: <div> test <div>
<Script>
Var div = document. getElementsByTagName ('div ') [0];
Alert (div. style. width );
</Script>

Empty strings are displayed in all browsers. Even if the style is embedded in the css of the page, it still cannot be obtained, as shown below:Copy codeThe Code is as follows: <style>
Div {width: 100px}
</Style>
<Div> test <div>
<Script>
Var div = document. getElementsByTagName ('div ') [0];
Alert (div. style. width );
</Script>

GetComputedStyle or currentStyle will be used in this case.
Scenario 2: The element sets the width/height by introducing a style sheet.
There are two ways to introduce a style sheet. One is to use the link tag to introduce a separate css file, and the other is to directly use the style tag on the html page. The second method is used for testing. As follows:Copy codeThe Code is as follows: <style>
Div {width: 100px}
</Style>
<Div> test <div>
<Script>
Function getStyle (el, name ){
If (window. getComputedStyle ){
Return window. getComputedStyle (el, null );
} Else {
Return el. currentStyle;
}
}
Var div = document. getElementsByTagName ('div ') [0];
Alert (getStyle (div, 'width '));
</Script>

100px is displayed in all browsers. GetComputedStyle and currentStyle are used to obtain the width and height of the element defined in the style sheet.
If the element is not set in the style attribute or in the style table, can it be obtained using getComputedStyle or currentStyle? The answer is that getComputedStyle is acceptable, but currentStyle is not. As follows:Copy codeThe Code is as follows: <div> test <div>
<Script>
Function getStyle (el, name ){
If (window. getComputedStyle ){
Return window. getComputedStyle (el, null );
} Else {
Return el. currentStyle;
}
}
Var div = document. getElementsByTagName ('div ') [0];
Alert (getStyle (div, 'width '));
</Script>

Div neither sets the style attribute nor introduces the style table. In Firefox/IE9/Safari/Chrome/Opera, you can obtain the width and height (default in the browser). However, IE6/7/8 does not work, and auto is returned.
Note that the getStyle method takes precedence over getComputedStyle, which is supported by IE9. Therefore, the width and height can be obtained in IE9. However, IE6/7/8 is not supported and can only be obtained using currentStyle.
Scenario 3: The element neither sets the style attribute nor introduces the style table.Copy codeThe Code is as follows: <div> test <div>
<Script>
Function getStyle (el, name ){
If (window. getComputedStyle ){
Return window. getComputedStyle (el, null) [name];
} Else {
Return el. currentStyle [name];
}
}
Function getWH (el, name ){
Var val = name = "width "? El. offsetWidth: el. offsetHeight,
Which = name = "width "? ['Left', 'right']: ['top', 'bottom'];
// Display is none
If (val = 0 ){
Return 0;
}
For (var I = 0, a; a = which [I ++];) {
Val-= parseFloat (getStyle (el, "border" + a + "Width") | 0;
Val-= parseFloat (getStyle (el, "padding" + a) | 0;
}
Return val + 'px ';
}
Var div = document. getElementsByTagName ('div ') [0];
Alert (getWH (div, 'width '));
</Script>

The idea is simple: Get the offsetWidth/offsetHeight of the element, minus the element padding and border.

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.