How to Use javascriptgetComputedStyle to obtain and set the style _ javascript skills
Source: Internet
Author: User
In the past two days, I used the pop-up window js in the baidu space. It feels good, powerful, and very simple to solve several problems. The interface friendliness and beautification are also much better. Previously, it was a hard-state window. open (); if you are interested, search for "Baidu popup". Someone has provided comments, which are powerful.
The most interesting thing is to use javascript to get and set the style.
The DOM standard introduces the overwrite style table concept. When we use document. getElementById ("id "). style. when backgroundColor gets a style, it only obtains the background color set in the style attribute of id. If the style attribute in id does not set background-color, it returns NULL, that is to say, if the id uses the class attribute to reference an external style table, the background color set in this external style table, so sorry document. getElementById ("id "). style. backgroundColor is difficult to write. To obtain the settings in the external style sheet, you need to use the getComputedStyle () method of the window object. The Code writes window. getComputedStyle (id, null ). backgroundColor
But the compatibility problem comes again, so it is easy to write in firefox, but it is difficult to make it in IE
Both are compatible
Window. getComputedStyle? Window. getComputedStyle (id, null). backgroundColor: id. currentStyle ["backgroundColor"];
If the background color is obtained, the returned values in firefox and IE are different. In IE, the returned values are "# ffff99", while in firefox, the returned values are "rgb (238, 44, 34 )"
It is worth noting that window. getComputedStyle (id, null) is not allowed to set the style and can only be obtained. To set it, you must also write the id. style. background = "# EE2C21 ";
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.