Home > Web Front-end > JS Tutorial > Get js functions currentStyle(IE), defaultView(FF)_javascript tips for styles in css style sheets

Get js functions currentStyle(IE), defaultView(FF)_javascript tips for styles in css style sheets

WBOY
Release: 2016-05-16 18:10:34
Original
1419 people have browsed it

However, DOM.style can only access , so the style built into the tag, if the style is written in




Here is the test content




Click Test



==============================

Copy code The code is as follows:
function getStyle(elem, name) {
//If the attribute exists in style[ ], then it has been set recently (and is the current one)
if (elem.style[name])
return elem.style[name];
//Otherwise, try the IE method
else if (elem.currentStyle)
return elem.currentStyle[name];
//Or W3C method, if it exists
else if (document.defaultView && document.defaultView.getComputedStyle) {
//It uses the traditional "text-Align" style rule writing method instead of "textAlign"
name = name.replace(/([A-Z])/g,"-$1");
name = name.toLowerCase();
//Get the style object and get the value of the attribute (if it exists)
var s = document.defaultView.getComputedStyle(elem,"");
return s && s.getPropertyValue(name);
//Otherwise, you are using another browser
} else
return null;
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template