检查 jquery 元数据是否存在?
似乎无法找到有关此谷歌搜索的任何信息。希望这并不意味着因为你做不到。 我正在尝试检查元素上是否存在元数据。因此,如果我有一个像 这样的元素,通过我的点击功能,我将使用
$ (this).hasClass('myid')
或 $(this).hasClass({'myid'})
。可以这样做吗?
还可以动态添加元数据吗?如果我尝试 $(this).addClass({some:data}) ,它会将其添加到类中,但我无法使用metadata() 读取它。
Couldn't seem to find any info about this googling. Hope that doesn't mean because you can't do it.
I'm trying to check if metadata exists on an element. So if I have an element like <a href="#" class="myclass {myid:2,text:bla}">
, with my click function I would use $(this).hasClass('myid')
or $(this).hasClass({'myid'})
. Is it possible to do this?
Also can you add metadata dynamically? If I try $(this).addClass({some:data}), it adds it onto the class, but I can't read it with metadata().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你想使用 jQuery.data(name, [value]);
您可以像这样使用它来存储和检索任何元素上的数据。
Sounds like you want to use jQuery.data(name, [value]);
You can store and retrieve data on any element using it like this.
据我所知,该插件提取的元数据是只读的,并且它还缓存它构造的对象,这样它就不必重新解析属性值等。如果您想要一个读写解决方案,那么编写一个小函数来提取元数据并将其添加到 标准 jQuery 数据存储。那么,可以随意修改。 (实际上,如果有人还没有编写这样的插件,我会感到有点惊讶。)
As far as I can tell, the metadata extracted by that plugin is read-only, and it also caches the object it constructs so that it doesn't have to re-parse attribute values and such. If you want a read-write solution, it wouldn't be too hard to write a little function that would take the metadata extracted, and add it to the standard jQuery data store. That, then, can be modified at will. (Actually, I'd be sort of surprised if someone hadn't already written such a plugin.)