Compatible notation for Javascript:currentstyle and getComputedStyle

Source: Internet
Author: User
Tags blank page

Currentstyle: Gets the calculated style, also called the current style, the final style.

Pros: You can get the final style of an element, including the default value of the browser, rather than just the inline style, as a style, so it's more common. Note: You cannot get composite styles such as background property values, only a single style such as Background-color, and so on.

alert (Oabc.currentstyle);

Unfortunately, this is not a good thing to be supported by large browsers. To be precise, in my test browser, IE8 and opera 11 pop Up "Object Cssstyledeclaration"; FF 12, Chrome 14, Safari 5 pops up "undefined".

Although Currentstyle cannot be applied to all browsers, it is possible to differentiate between supported and unsupported browsers based on the results of the tests above, and then find compatible wording.

var oabc = document.getElementById ("abc"), if (Oabc.currentstyle) {//ie, Operaalert ("I Support Currentstyle"),} else {//ff, Chrome, Safarialert ("I don't support Currentstyle");}

In fact, in the FF browser we can use getComputedStyle (obj,false) to achieve the same effect as currentstyle under IE.

getComputedStyle (Obj,false): In the new version of FF only the first parameter, that is, the Operation object, the second parameter write "False" is also common use, the purpose is to be compatible with the old version of the Firefox browser.

Compatible wording:

var oabc = document.getElementById ("abc"), if (Oabc.currentstyle) {//ie, Opera//alert ("I Support Currentstyle"), Alert ( OAbc.currentStyle.width);} else {//ff, Chrome, Safari//alert ("I don't support Currentstyle"), Alert (getComputedStyle (oabc,false). width);}

A blank page in the body of the id= "ABC", test the above code, ie and opera pop "Auto", the other three browsers pop up "***px". Although the results are different, you can see that Chrome and Safari, like Firefox, have successfully read the property values. The three browsers (FF, Chrome, Safari) that do not support Currentstyle are supported by getComputedStyle.

The results show that whether the browser supports currentstyle judgment + getComputedStyle, it can be compatible with the effect of the main browsers. And the compatibility of the wording is not complicated, you have mastered it? ^_^

support Currentstyle:ie, Opera support for Getcomputedstyle:firefox, Chrome, Safari

Note the last popup, Currentstyle returns the default value "Auto" for the browser, and getComputedStyle returns the specific value "**PX"

Compatible notation for Javascript:currentstyle and getComputedStyle

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.