Home  >  Article  >  Web Front-end  >  JavaScript reads styles outside of inline (introduction to the differences between style, currentStyle, and getComputedStyle)_javascript skills

JavaScript reads styles outside of inline (introduction to the differences between style, currentStyle, and getComputedStyle)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:571032browse

There are three ways of style sheets
Inline Style: It is written in the Tag. Inline Style is only valid for all Tags.
Internal Style Sheet: It is written in the of HTML. The internal style is only valid for the web page where it is located.
External Style Sheet: If many web pages need to use the same styles (Styles), write the styles (Styles) in a CSS file with the suffix .css, and then use it on each page This CSS file is referenced in the web pages of these styles.

The following is js for reading styles other than inline styles. It can only be read but not written.

Copy code The 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, under FF, you can also obtain it through the following method
document.defaultView.getComputedStyle(mydiv,null).width
window.getComputedStyle(mydiv , null).width


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn