JS uses getComputedStyle () method to get CSS property value _javascript Tips

Source: Internet
Author: User
In the process of debugging a Web page, often use JS to get elements of CSS style, there are many ways, now only the method I often use summarized as follows:

1. Obj.style: This method can only be JS to get the value written in the style attribute in the HTML tag (style= "..."), and cannot get the properties defined in <style type= "Text/css" >.
Copy Code code as follows:

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8″/>
<title>js Get CSS Property value </title>
<style type= "Text/css" >
<!–
. Ss{color: #cdcdcd;}
–>
</style>

<body>
<div id= "css88″class=" ss "style=" width:200px; height:200px; Background: #333333 ″>js get CSS property value </div>
<script type= "Text/javascript" >
Alert (document.getElementById ("Css88″). Style.width);//200px
Alert (document.getElementById ("Css88″). Style.color);/blank
</script>
</body>

2. IE is used in the Obj.currentstyle method, and FF is the getComputedStyle method

The DOM2 level style enhances the Document.defaultview and provides a getComputedStyle () method. This method takes two parameters: the element to get the calculated style and a pseudo-element string (for example, after). If pseudo element information is not required, the second argument can be null. The Getcomputerstyle () method returns a Cssstyledeclaration object that contains all the calculated styles of the current element. Take the following HTML page for example:
Copy Code code as follows:

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><! DOCTYPE html>
<title> Calculate element Styles </title>
<style>
#myDiv {
Background-color:blue;
width:100px;
height:200px;
}
</style>
<body>
<div id = "Mydiv" style= "background-color:red"; border:1px solid Black "></div>
<script>
var mydiv = document.getElementById ("mydiv");
var Computedstyle = Document.defaultView.getComputedStyle (mydiv, NULL);
alert (Computedstyle.backgroundcolor); "Red"
alert (computedstyle.width); "100px"
alert (computedstyle.height); "200px"
alert (Computedstyle.border); In some browsers it is "1px solid black"
</script>
</body>

The border property may also not return the actual border rule in the stylesheet (opera will return but not other browsers). The reason for this difference is that different browsers interpret composite properties differently because setting this property actually involves many other properties. When you set up border, you are actually setting the border width, color, and style properties for four sides. Therefore, even if Computedstyle.border does not return a value in all browsers, Computedstyle.borderleftwidth returns a value.

It should be noted that even though some browsers support this feature, there may be a difference in how the value is expressed. For example, Firefox and Safari return to convert all colors to RGB format. Therefore, even if the getComputedStyle () method, it is best to test in several browsers.

IE does not support the getComputedStyle () method, but it has a similar concept. In IE, each element with the style attribute has a Currentstyle property. This property is an instance of cssstyledeclaration that contains the calculated style for the current element. These styles are obtained in the same way as follows:
Copy Code code as follows:

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >var mydiv = document.getElementById ("mydiv");
var computedstyle = Mydiv.currentstyle;
alert (Computedstyle.backgroundcolor); "Red"
alert (computedstyle.width); "100px"
alert (computedstyle.height); "200px"
alert (Computedstyle.border); Undefined</span>

As with the DOM version, IE does not return the border style because it is a composite property.

3. A simple function I wrote in my own test case (for Chrome):
Copy Code code as follows:

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >function Getcss (Div) {
Return Document.defaultView.getComputedStyle (div, null);
Return div.currentstyle;//no use, IE
}</span>
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.