Implementation Code _ javascript

Source: Internet
Author: User
For the sake of simplicity, we only use the width example here. Get the offsetWidthoffsetHeight of the element, minus the padding and border of the element. Scenario 1: The width/height attribute of the element style is set

The Code is as follows:


Test


Script
Var p = document. getElementsByTagName ('P') [0];
Alert (p. 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.

The Code is as follows:


Test


Script
Var p = document. getElementsByTagName ('P') [0];
Alert (p. 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:

The Code is as follows:



Test


Script
Var p = document. getElementsByTagName ('P') [0];
Alert (p. 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:

The Code is as follows:



Test


Script
Function getStyle (el, name ){
If (window. getComputedStyle ){
Return window. getComputedStyle (el, null );
} Else {
Return el. currentStyle;
}
}
Var p = document. getElementsByTagName ('P') [0];
Alert (getStyle (p, '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:

The Code is as follows:


Test


Script
Function getStyle (el, name ){
If (window. getComputedStyle ){
Return window. getComputedStyle (el, null );
} Else {
Return el. currentStyle;
}
}
Var p = document. getElementsByTagName ('P') [0];
Alert (getStyle (p, 'width '));
Script


P neither sets the style attribute nor introduces the style sheet. 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.

The Code is as follows:


Test


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 p = document. getElementsByTagName ('P') [0];
Alert (getWH (p, '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.