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 ).