This article summarizes several methods to obtain the size and position of a specified element using JavaScript. If you use JQuery, it is very easy to get the element size. However, we still need to know how to obtain it through Native JavaScript. For more information, see
1. Obtain the row style of an element
The Code is as follows:
Var obj = document. getElementById ("test ");
Alert (obj. height + "\ n" + obj. width );
// 200px 200px typeof = string only displays the value in the style attribute
2. Obtain the calculated Style
The Code is as follows:
Var obj = document. getElementById ("test ");
Var style = null;
If (window. getComputedStyle ){
Style = window. getComputedStyle (obj, null); // non-IE
} Else {
Style = obj. currentStyle; // IE
}
Alert ("width =" + style. width + "\ nheight =" + style. height );
Note: If the width and height of an element are not set, the default width and height are returned in a non-IE browser. Returns the auto string under IE.
3. Obtain And