简体   繁体   中英

Can I access a non-existent or non-valid css property with jQuery?

There are 2 parts to this question:

  1. If I define a CSS transition and try to access it with jQuery's .css() method, can I reliably assume that if the browser I'm running on does not support that CSS property it will always return undefined ?

  2. If I DON'T define a CSS property and try to access it with jQuery's .css() method, can I reliably assume that it will always return undefined ?

The short answer is, #1. yes. #2. No.

The long answer is: #1. yes, you can make up a classname to test this, for example:

browser-foo: blue;

http://jsfiddle.net/32dzN/1/

If you haven't already, check out Modernizr. They provide excellent feature detection, I believe they put it into a DOM element off the screen if I recall correctly.

As for #2, that's not how it works at all. Because there are computed styles, as well as defined styles. For example, if you try to get the opacity of an element that you haven't set opacity on, it will return as 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM