Javascript reads styles other than inline (differences between style, currentStyle, and getComputedStyle)

Source: Internet
Author: User

Style sheets can be created in three ways:
Inline Style: it is written in the Tag. The embedded Style is only valid for all tags.
Internal Style Sheet: it is written in the HTML External Style Sheet: if many web pages are used in the same Style (styles), write the Style (styles(in a CSS file suffixed with .css, and then reference the CSS file on each page that requires these Styles.

The following is a javascript code that reads styles other than embedded styles. It can only be read and cannot be written.
Copy codeThe Code is as follows:
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 addition, you can use the following method to obtain
Document. defaultView. getComputedStyle (mydiv, null). width
Window. getComputedStyle (mydiv, null). width

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> Document </title> <style >#test {width: 200px ;} </style> </pead> <body> this is test content </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

The most commonly used is the style attribute. In JavaScript, the document. getElementById (id ). style. XXX can get the value of XXX, but unexpectedly, only the style value set in the embedded mode can be obtained, that is, the value set in the style attribute.

Solution: Introduce currentStyle, runtimeStyle, getComputedStyle
Standard style! It may be specified by the style attribute!
RuntimeStyle runtime style! If it overlaps with the style attribute, it overwrites the style attribute!
CurrentStyle refers to the combination of style and runtimeStyle!
You can use currentStyle to obtain the CSS style value that is referenced by inline or external reference (IE only)
For example, document. getElementById ("test"). currentStyle. top
To be compatible with FF, getComputedStyle is required.
Note: getComputedStyle is in firefox,
CurrentStyle is in ie.
For example
Copy codeThe Code is as follows:
<Style>
# Mydiv {
Width: 300px;
}
</Style>

Then:
Copy codeThe Code is as follows:
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 addition, you can use the following method to obtain
Copy codeThe Code is as follows:
Document. defaultView. getComputedStyle (mydiv, null). width
Window. getComputedStyle (mydiv, null). width

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.