Using native JavaScript to get element styles just get it. _javascript Tips

Source: Internet
Author: User

PS: is to get the style, not to set the style. If no style value is set for the element, the default value given by the browser is returned. (Forum finishing)

1. Element.style: can only get the style values written in the style attribute in the element label, cannot get to the definition in <style></style> and through <link href= "Css.css" > Load in style properties

Copy Code code as follows:

var ele = document.getElementById (' ele ');
Ele.style.color; Get color

2, window.getComputedStyle (): Can get the current element of all the final use of CSS property values.

Copy Code code as follows:

window.getComputedStyle ("element", "pseudo class");

This method takes two parameters: the element to get the calculated style and a pseudo-element string (for example, ": Before"). If pseudo element information is not required, the second argument can be null. You can also use the Document.defaultView.getComputedStyle ("element", "pseudo class");

Copy Code code as follows:

var ele = document.getElementById (' ele ');
var styles = window.getComputedStyle (ele,null);
Styles.color; Get color

You can view the number of browser default styles by Style.length. Ie6-8 does not support this method, you need to use the following method. For Firefox and Safari, colors are converted to RGB format.

3, Element.currentStyle:IE dedicated, return is the element of the current application of the final CSS attribute values (including the external chain CSS file, the page embedded <style> properties, etc.).

Copy Code code as follows:

var ele = document.getElementById (' ele ');
var styles = Ele.currentstyle;
Styles.color;

Note: For integrated properties border, ie returns undefined, other browsers have return values, some do not return, but borderleftwidth such attributes are returned values

4, GetPropertyValue (): Get the Direct property name of the CSS style

Copy Code code as follows:

var ele = document.getElementById (' ele ');
window.getComputedStyle (Ele,null). GetPropertyValue (' color ');

Note: The property name does not support the hump format, IE6-8 does not support this method, you need to use the following method

5, GetAttribute (): Similar to the GetPropertyValue, there is a little difference is the attribute name hump format

Copy Code code as follows:

var test = document.getElementById (' test ');
window.getComputedStyle (test, NULL). GetPropertyValue ("BackgroundColor");

Note: This method only supports ie6-8.

The following get style methods compatible with IE, Chrome, Firefox, etc.

Copy Code code as follows:

function GetStyle (ele) {

var style = null;

if (window.getComputedStyle) {

style = window.getComputedStyle (ele, NULL);

}else{

style = Ele.currentstyle;

}
return style;
}

In jquery, the common CSS () gets the style attributes, and the underlying operation uses the getComputedStyle and GetPropertyValue methods.

Related Article

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.