How to Get element size and size in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
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

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.