Use JavaScript getComputedStyle to get and set the principle of style _javascript tips

Source: Internet
Author: User
Interested friends Search "Baidu popup" is good, has been given a note, strong.
The most interesting thing is to use JavaScript to get and set style

The DOM Standard introduces the concept of overriding stylesheets when we use document.getElementById ("id"). Style.backgroundcolor gets the style only when it gets the background color set in the Style property in the ID. If Background-color is not set in the style attribute in the ID, then it returns empty, that is, if the ID uses the class attribute to refer to an external style sheet, the background color set in the external style sheet, So sorry. document.getElementById ("id"). Style.backgroundcolor this is not the case, if you want to get the settings in the external style sheet, you need to use the Window object's getComputedStyle ( method, the code writes window.getComputedStyle (id,null). backgroundcolor
But the compatibility problem comes again, so write in Firefox, but not in IE
Two compatible ways to write
Window.getcomputedstyle?window.getcomputedstyle (id,null). backgroundcolor:id.currentstyle["BackgroundColor"];
If you get the background color, this method in Firefox and IE return value is not the same, IE is returned to the "#ffff99" appearance, and Firefox returned "RGB (238, 44, 34)"
It is noteworthy that: window.getComputedStyle (id,null) This way can not set the style, can only be obtained, to be set up to be written like this id.style.background= "#EE2C21";

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.