简体   繁体   English

我可以使用jQuery访问不存在或无效的CSS属性吗?

[英]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 ? 如果我定义了CSS转换并尝试使用jQuery的.css()方法访问它,我是否可以可靠地假定,如果我运行的浏览器不支持该CSS属性,它将始终返回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 ? 如果我定义CSS属性并尝试使用jQuery的.css()方法访问它,我是否可以可靠地假定它始终返回undefined

The short answer is, #1. 简短的答案是#1。 yes. 是。 #2. #2。 No. 没有。

The long answer is: #1. 长答案是:#1。 yes, you can make up a classname to test this, for example: 是的,您可以组成一个类名来对此进行测试,例如:

browser-foo: blue;

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

If you haven't already, check out Modernizr. 如果您还没有的话,请查看Modernizr。 They provide excellent feature detection, I believe they put it into a DOM element off the screen if I recall correctly. 它们提供了出色的功能检测,如果我没记错的话,我相信他们会将其放入屏幕上的DOM元素中。

As for #2, that's not how it works at all. 至于#2,那根本不是它的工作原理。 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. 例如,如果您尝试获取未设置不透明度的元素的不透明度,则该元素的返回值为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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