External reference CSS properties in JavaScript how to get

Source: Internet
Author: User

Today, when it comes to encapsulating a library , I find that I want to get the CSS property of the node, if we don't write the style of the line, but write it through link, we want to pass the. Style. property in JavaScript , which is not available.

Looked up on the internet, The Web provides a method:window.getComputedStyle () can get the calculation of the node's style, the method has two parameters, the first is to get the style of the node, The second parameter does not know what the effect is, and the example given on the web will be set to NULL, that is, a collection of property value pairs that are called window.getComputedStyle (Node,null), whose return value is an object, for the computed style.

However, the method is not supported in IE , but in IE, the element node has a property corresponding to the method that the node calculates, for example, in IE, the node is computed in the following style:Node.currentstyle, This property is a collection of property value pairs that are an object and also a computed style. For compatibility we can rewrite its package to provide a unified approach to Getcurrentstyle (node)

To show an example:

argument node: The element node that will get its calculated style, function Getcurrentstyle (node) {    nullifnull);} else{style =return style;}          

The following code is the Style property value that gets the display of the DIV:

1 <div id= "div" >div node </div>   4 <script>var div = document.getElementById ("div");  var style = Getcurrentstyle (div);  var display = style["display"];  output: Block</script>         

External reference CSS properties in JavaScript how to get

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.