Summary of ways to get styles using native JavaScript

Source: Internet
Author: User

Source: http://www.ido321.com/930.html

PS: is the get style. Not set style. If no style value is set for the element. Returns the default value given by the browser. (Forum finishing)

1, Element.style: Only can get the style value that is written in the element tag, cannot get to the definition in <style></style> and through <link href= "Css.css" > Style properties Loaded in

 1:  var  ele = document.getElementById ( ' ele ' );  
   2:  ele.style.color;    Get Color

2, window.getComputedStyle (): Ability to get all the CSS property values that are finally used by the current element.

   1:  window.getcomputedstyle ("element""Pseudo-Class");

This method accepts two parameters: to get the element of the calculated style and a pseudo-element string (such as ": Before").

Assuming that no pseudo-element information is required, the second parameter can be null.

can also be used by Document.defaultView.getComputedStyle ("element", "Pseudo-class");

 1:  var  ele = document.getElementById ( ' ele ' );  
 2:  var  styles = window.getComputedStyle (Ele,null );
   3:  styles.color;  Get Color

The ability to see the number of default browser styles through Style.length. Ie6-8 does not support this method, you need to use the following method. For Firefox and Safari. The color is converted to RGB format.

3, Element.currentStyle:IE dedicated. Returns the value of the element's currently applied CSS property (including the outer-chain CSS file, the <style> properties embedded in the page, and so on).

 1:  var  ele = document.getElementById ( ' ele ' );  
 2:  var  styles = Ele.currentstyle;  
   3:  styles.color;

Note: For synthetic attribute border, etc. ie returns undefined, other browsers have return values. Some do not return. But borderleftwidth this property is the return value of the

4. GetPropertyValue (): Gets the direct property name of the CSS style

 1:  var  ele = document.getElementById ( ' ele ' );  
   2:  window.getcomputedstyle (ele,null). GetPropertyValue (' color ');

Note: The property name does not support camel formatting. Ie6-8 does not support this method. You need to use the following methods

5, GetAttribute (): Similar to GetPropertyValue, there is a little difference is the property name hump format

 1:  var  test = document.getElementById ( ' test ' );  
   2:null). GetPropertyValue ("BackgroundColor");

Note: This method only supports ie6-8.

The following methods of obtaining styles are compatible with IE, Chrome, Firefox, etc.

 1:  function  getStyle (ele) { 
 2:  var  style = null ;  
 3:  
 4:  if  (window.getComputedStyle) { 
 5:  null );  
 6:  else  { 
 7:   
   8:     }
   9:     
:   return  style;  
One   :  }

In jquery, you often use CSS () to get style properties. The getComputedStyle and GetPropertyValue methods are applied to the underlying operations.

Summary of ways to get styles using native JavaScript

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.