JavaScript get element style must kill skill

Source: Internet
Author: User
Tags html tags

This article mainly introduces the JavaScript to get the element style must kill the skill, the need friend may refer to under

JavaScript gets CSS property value method: getComputedStyle and Currentstyle

1. Inline CSS style for elements (

Hello

Can directly use Element.style.color to get the value of CSS property directly;

2. However, external-defined CSS styles are not available in this way, and IE browsers and other standard browsers (Firefox,chrome,opera,safari) use different methods. The IE browser uses the ELEMENT.CURRENTSTYLE,W3C standard browser to use the getComputedStyle to obtain.

1. IE gets the CSS attribute values defined externally by the element: Element.currentstyle

The Currentstyle object returns the style sheet on the element, but the style object returns only the inline style applied to the element through the style label property.

Therefore, the style values obtained through the Currentstyle object may differ from the style values obtained through the style object.

For example, if the Color property value of a paragraph is set to red instead of inline with a linked or embedded style sheet, the object. Currentstyle.color returns the correct color, and the object Style.color cannot return a value. However, if the user specifies, the Currentstyle and Style objects will return the value red.

The Currentstyle object reflects the style precedence in the style sheet. In HTML, this order is:

1) Inline style

2) style sheet rules

3 HTML Tag Properties

4 internal definition of HTML tags

2. The consortium gets the CSS attribute values that are defined externally by the element: window.getComputedStyle (element, Pseudoelt)

Element must be selected, HTML element

Pseudoelt required to get the pseudo class style of the element

The code is as follows:

function GetStyle (node, property) {

if (Node.style[property]) {

return Node.style[property];

}

else if (Node.currentstyle) {

return Node.currentstyle[property];

}

else if (Document.defaultview && document.defaultView.getComputedStyle) {

var style = Document.defaultView.getComputedStyle (node, null);

Return Style.getpropertyvalue (property);

}

return null;

}

Note : Please pay attention to the triple Programming Tutorials section for more wonderful articles .

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.