【问题标题】:How to access js object properties via html element?如何通过 html 元素访问 js 对象属性?
【发布时间】:2011-01-16 22:54:38
【问题描述】:

我有一个使用 jQuery.getJSON() 获取的对象数组。我希望我的每个对象都由 HTML div 元素表示,因此当您单击该元素时,您可以访问相应对象的所有属性。最好的方法是什么?

我想这样做:

$('.mydiv').click(function() {
   var id = $(this).attr('id');
   for (i=0; i<myObjectsArray.length; i++){
     for (x in myObjectsArray[i]){
         //..and here I got confused...
     }  
   }
});

这种方法有什么好处还是有更好的方法。 谢谢。

【问题讨论】:

  • 如果不了解 myObjectsArray 的内容,我很难理解您的问题/建议某种解决方案。安排它以便 myObjectsArray[id] 可以访问它我可能是个好主意。

标签: javascript jquery html object arrays


【解决方案1】:

您可以将数据存储在 jQuery 对象的 data 属性中
docs here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多