window.getComputedStyle to get pseudo-class element styles

Source: Internet
Author: User

window.getComputedStyledetailedwindow.getComputedStyle

Description:
getComputedStyle () returns the computed value of all CSS properties for an element

Syntax:
var style = window.getComputedStyle (element[, Pseudoelt]);

parameter Description :
element: The DOM object of the target element
Pseudoelt: Indicates the pseudo-element to be matched, must be specified as null (or omitted) for the normal element (or not specified is not known to have a problem, but the test result indicates that the parameter can be omitted for normal elements)

the return value of style is the Cssstyledeclaration object.

Note : The Pseudoelt parameter is required in the previous version of Gecko2.0 (Firefox 4/thunderbird 3.3/seamonkey 2.1). For other mainstream browsers, If the parameter is specified as NULL, it can be omitted. Gecko later also changed to be consistent with other browsers.

Test :
<a href= "#" id= "Showandhide" >show and hide</a>
<script type= "Text/javascript" src= "./jquery-1.8b1.js" ></script>
<script>
$ (' #showAndHide '). Click (function () {
var dom = this;
Console.log (getComputedStyle (DOM));
});
</script>

Example 1:
var elem1 = document.getElementById ("Elemid");
var style = window.getComputedStyle (elem1, null);

//This is equivalent:
//var style = Document.defaultView.getComputedStyle (elem1, null);

Example 2:
<style>
#elem-container{
Position:absolute;
left:100px;
top:200px;
height:100px;
 }
</style>

<div id= "Elem-container" >dummy</div>
<div id= "Output" ></div>

<script>
function Getthestyle () {
var elem = document.getElementById ("Elem-container");
var thecssprop = window.getComputedStyle (elem,null). GetPropertyValue ("height");
document.getElementById ("Output"). InnerHTML = Thecssprop;
   }
Getthestyle ();
</script>

There is a section in the official document about the difference between the getComputedStyle () and the style attribute of the element


Description

The returned object is of the same type, the object returned from the element's style property, however the object S has different purpose. The object returned from getComputedStyle are read-only and can be used to inspect the element ' s style (including those set By a <style> element or an external stylesheet). The Elt.style object should is used to set styles on a specific element.

This passage is just a statementgetComputedStyle gets a value that is read-only and can be used to detect the style of the element (including the style attribute and the external style). The Elt.style can be used to set the style of the specified element.

inFor example, consider the difference between getComputedStyle () and the style property of an element, and notice the difference between the Csstext property
<a href= "#" id= "Showandhide" >show and Hide</a>
<script type= "Text/javascript" src= "./jquery-1.8b1.js" ></script>
<script>
$ (' #showAndHide '). Click (function () {
var dom = this;
Console.log (getComputedStyle (DOM));
Console.log (Dom.style);
});
</script>

DefaultView

in a lot of onlineDomo, getComputedStyle is always used as a method of Document.defaultview, but this is not necessary. Because getComputedStyle is present in the Window object. Using the Firefox 3.6 access frame style is the only place where you must use DefaultView.

Get pseudo element(such as: After,: Before,: marker,: line-marker) style
directly to the officialDemo:
<style>
H3:after {
Content: ' rocks! ';
}
</style>


<script>
var h3 = Document.queryselector (' h3 '),
result = getComputedStyle (H3, ': After '). Content;

Console.log (' The generated content is: ', result); Returns ' Rocks! '
</script>

window.getComputedStyle to get pseudo-class element styles

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.