Summary of native methods for getting styles in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the summary of native methods for getting styles in JavaScript. This article summarizes five native methods for getting styles in JavaScript and provides examples respectively. If you need them, refer to ps: is to get the style, not to set the style. If no style value is set for the element, the default value is returned. (Forum arrangement)

1. element. style:Only the style values written in the style attribute in the element label can be obtained.And pass Loaded style attributes

The Code is as follows:


Var ele = document. getElementById ('ele ');
Ele. style. color; // obtain the color.

2. window. getComputedStyle ():You can obtain all final CSS attribute values of the current element.

The Code is as follows:

Window. getComputedStyle ("element", "pseudo class ");


This method accepts two parameters: the element to obtain the calculated style and a pseudo element string (for example, ": before "). If the pseudo element information is not required, the second parameter can be null. You can also use document. defaultView. getComputedStyle ("element", "pseudo class ");

The Code is as follows:


Var ele = document. getElementById ('ele ');
Var styles = window. getComputedStyle (ele, null );
Styles. color; // obtain the color.


You can use style. length to view the number of default styles in the browser. The IE6-8 does not support this method and requires the following method. For Firefox and Safari, the color is converted to rgb format.

3. element. currentStyle:The returned result is the final CSS attribute value of the current element application (including the external link CSS file, embedded in the page ).

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.