如何根据数据属性内对象的属性选择元素? [英] How to select an element based on the property of an object inside a data attribute?
本文介绍了如何根据数据属性内对象的属性选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我的标记如下所示:
If my markup looks like this:
<div data-test="{ "value" : "bar", "_id" : 1234, "name" : "john", "age" : 25 }">...</div>
<div data-test="{ "value" : "foo", "_id" : 1235, "name" : "paul", "age" : 26 }">...</div>
<div data-test="{ "value" : "drummer", "_id" : 1236, "name" : "ringo", "age" : 22 }">...</div>
如果我只有键"bar"或"foo",我该如何使用JQuery选择特定元素?
How would I select a particular element using JQuery if I only had the key 'bar' or 'foo'?
我可以拉出每一行的整个对象,然后遍历整个对象以寻找匹配项,但是如果有更有效的方法,我宁愿不这样做.
I could pull out the whole object for each row and iterate through it looking for a match but I'd rather not if there is a more efficient method.
如何根据对象的属性进行干净选择?
How can I cleanly select based on the property of an object?
推荐答案
您可以使用:
$("div").filter(function() {
return $(this).data("test").value == "bar";
})
jQuery.data()
自动将JSON格式的数据值解析为相应的对象.
jQuery.data()
automatically parses a data value that's in JSON format into the corresponding object.
这篇关于如何根据数据属性内对象的属性选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文