检查jQuery对象中特定.data()值的正确方法? [英] Proper way to check for particular .data() value in a jQuery object?
问题描述
我想检查一下是否已将jQuery数据值应用于对象.例如,其他地方的代码可能会将数据分配给对象:
I'd like to check to see if a jQuery data value has been applied to an object. For instance, code elsewhere may assign data to an object:
$myObject.data('hello','world');
在其他地方,我想检查是否为数据属性hello赋了值.
Elsewhere, I want to check to see if a value was given to the data property hello.
处理此问题的正确方法是什么? .hasData
似乎是显而易见的选择,但似乎不像'hasClass'那样工作,因为您似乎无法向其传递特定的数据元素名称.
What is the proper way to handle this? .hasData
seemed like the obvious choice but appears to not work like 'hasClass' does in that you can't seem to pass a particular data element name to it.
推荐答案
要获取属性(例如,用于比较),请使用.data
和一个参数:
To obtain the property (e.g. for comparison), use .data
with one argument:
$myObject.data('hello')
当数据不存在时,它返回undefined
值.
It returns the undefined
value when the data does not exists.
这篇关于检查jQuery对象中特定.data()值的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!