我的页面上有一个包含多个<ul>
的窗格,在每个有序列表中,其中一个列表项是已经字符串化的lat-long对象。当我点击有序列表标题时,我需要将字符串转换为JSON对象。
$.each(dealers, function (i, obj) {
$listSelector.append("<ul class='dealer-summary-list'>" + "<li class='dealerName'>" + obj.title + "</li>" + "<li class='dealer-summary-listItem'>" + obj.address + "</li>" + "<li class='dealer-summary-listItem'>" + obj.locale + "</li>" + "<li class='dealer-summary-listItem'>" + obj.location.lat + ', ' + obj.location.lng + "</li>" + "<li class='dealer-summary-listItem'>" + obj.phone + "</li>" + "</ul>")
});
答案 0 :(得分:1)
为您的UL元素分配一个自定义属性,指示经销商阵列中相应经销商的索引。
不是解析li元素,而是获取自定义属性并使用它从数组中检索经销商对象
例如在元素创建脚本
中 $.each(dealers, function (i, obj) {
$listSelector.append("<ul dealerindex='"+i+ "' class='dealer-summary-list'>" + "<li class='dealerName'>" + obj.title + "</li>" + "<li class='dealer-summary-listItem'>" + obj.address + "</li>" + "<li class='dealer-summary-listItem'>" + obj.locale + "</li>" + "<li class='dealer-summary-listItem'>" + obj.location.lat + ', ' + obj.location.lng + "</li>" + "<li class='dealer-summary-listItem'>" + obj.phone + "</li>" + "</ul>")
});
在解析脚本中 var idx = parseInt($(this).attr(&#34; dealerindex&#34;)); var location = dealer [idx] .location;