JS Gets the property style of the DOM element __JS

Source: Internet
Author: User
There are three ways to style sheets

inline style (inline style): It is written in tag, the inline style is only valid for all tags.

Internal style (internal style Sheet): is written inside the HTML, the internal style is only valid for the Web page in which it is located.

External style sheet (External style Sheet): If many pages need to use the same style (Styles), write the style (Styles) in a CSS file with a. css suffix, and then use these styles for each (Styles) Page to refer to this CSS file. The most commonly used is the style property, in JavaScript, through the document.getElementById (ID). style. XXX can get the value of XXX, but unexpectedly, it can only take the style value set inline, that is, the value set in the Style property.


Solution: Introduce the style of the Currentstyle,runtimestyle,getcomputedstyle style standard, which may be specified by the Style property.

Runtimestyle the style at run time. If you overlap the properties of the style, the properties of the style are overwritten.

Currentstyle refers to the combination of style and runtimestyle. By Currentstyle, you can obtain values for CSS styles that are inline or externally referenced (ie only) such as: document.getElementById ("Test"). Currentstyle.top

To be compatible with FF, you need to getcomputedstyle.

Note: getComputedStyle is in Firefox, Currentstyle is in IE. For example

1 2 3 4 5 <style> #mydiv {width:300px;} </style>

The

1 2 3 4 5 6 7 8 var mydiv = document.getElementById (' mydiv ');        if (Mydiv.currentstyle) {var width = mydiv.currentstyle[' width ']; Alert (' ie: ' + width);        else if (window.getComputedStyle) {var width = window.getComputedStyle (mydiv, null) [' width ']; Alert (' Firefox: ' + width '); }

In short, JS gets the style values of the elements, compatible with multiple browsers, can be obtained as follows:

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.