JavaScript gets the calculated style

Source: Internet
Author: User

How to get the standard browser

1 varOdiv = document.getElementById (' div ');2 //get the computed font-size3 varSFs = getComputedStyle (Odiv,NULL). GetPropertyValue (' font-size ');//Recommended Use4 //or5 varSfs= getComputedStyle (Odiv,NULL). FontSize;//not recommended for use6 7 Console.log (sFs);8 9 //Note 1: Because of the different names of CSS properties in different browsers (such as Get float), some browsers are. Cssfloat, some are. stylefloat, in order to avoid browser judgment, use. GetPropertyValue (' Float ') to avoid this problem. Ten //NOTE 2: The values in the. GetPropertyValue () method do not support hump writing, such as FontSize can only be written as GetPropertyValue (' font-size ') One}

IE9 How to obtain the following browsers:

1 var odiv = document.getElementById (' div '); 2 // get the computed font-size 3 var // Recommended Use 4 // or 5 var // not recommended for use 6 7 console.log (sFs)

Encapsulation gets the calculated style method

1 functionGetStyle (node,attr) {2     if(typeofgetComputedStyle! = ' undefined '){3         varValue = getComputedStyle (node,NULL). GetPropertyValue (attr);4         returnattr = = ' opacity '? Value * 100:VALUE;//compatible opacity, if it is opaque, returns an integer for easy calculation5}Else if(typeofNode.currentstyle! = ' undefined '){6         if(attr = = ' opacity ') {//compatible opacity7             returnNumber (Node.currentStyle.getAttribute (' Filter '). Match (/(?: O Pacity[=:]) (\d+)/) [1]);8}Else{9             returnNode.currentStyle.getAttribute (attr);Ten         } One     } A}

JavaScript gets the calculated style

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.