如何解决使用类似WHERE的方法搜索对象?
| 我有一个来自服务的对象结构:var response ={\"results\": [
{\"name\":\"peter\",\"habits\":\"smoking\",\"imgID\":\"something_unique\"},{\"name\":\"paul\",\"habits\":\"drinking\",{\"name\":\"patrick\",\"habits\":\"gambling\",\"imgID\":\"something_unique\"}
]}
我迭代对象并创建由click事件监听的div:
for(var prop in response) {
if(response.hasOwnProperty(prop))
{
this.div.insert(new Element(\'div\',{\'className\': \'\' })
.insert(\'<img id=\"\'+imgID+\'\" src=\"/img.jpg\" width=70 height=70 /> \')
);
在回调中,我可以从对象生成的事件中访问ID:
function callback (event) { console.log(event.id) ; var compare_ID=event.id }
但是,在我的回调中,我需要获取被单击的那个特定对象的数组var名称。因此,从概念上讲,我希望这样做:
在回调中,迭代response
对象,然后选择results[i]
,其中imgID
是== event.id return response
对象以供进一步访问,而不仅仅是字符串名称。
谢谢,并且一如既往地感谢所有帮助
解决方法
您可以使用data- *属性存储对象的编号或键或实际值。我认为这会有所帮助。
例如。:
<img id=\"something_unique\" src=\"/img.jpg\" width=70 height=70 data-result-number=\"1\" />
要么
<img id=\"something_unique\" src=\"/img.jpg\" width=70 height=70 data-name=\"peter\" data-habits=\"smoking\" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。