Get CSS properties of Style getComputedStyle Currentstyle three different ways to summarize

Source: Internet
Author: User

There are three styles in CSS, which are inline style inner style external style sheet

1.style

Syntax: element.style.xxx This can only get the inline style properties, get the style can read can write

2.currentStyle

Syntax: element.currentstyle[xxx] can get internal and external styles, but only IE browser compatible, get the style can only read

Example: Document.createlement (' div '). currentstyle[' width '] gets the width of the element

3.getComputedStyle

Syntax: window.getComputedStyle (element,null). XXX Document.defaultView.getComputedStyle (element,null). xxx

function and currentstyle the same but not for IE browser, you will get all the original elements of the element, even if the element is not set any style, the second way of writing from the jquery source code, is the. css () the underlying operation method.

Can write a function that is compatible with all browsers to get its style

function GetStyle (element,property) {          return Document.defaultView.getComputedStyle? Document.defaultView.getComputedStyle (element,false) [property]: Element.currentstyle[property];}

Get CSS properties of Style getComputedStyle Currentstyle three different ways to summarize

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.