2013-12-23 25 views
0

我想在AJAX响应中添加一些html(li元素)到DOM中的一些数据。但它似乎并不正确,因为我无法在Firebug中找到它或找回它。在存储在变量中的html上设置隐藏数据

我不知道如何成功地设置数据,而不使用html中的数据属性,我不想这样做。

$.each(r.results, function(i, item) { 
$tpl = '<li>'; 
$tpl += '<figure>'; 
$tpl += '<img src="' + item.image + '" alt="' + item.name + '">'; 
$tpl += '</figure>'; 
$tpl += '</li>'; 
$($tpl).find('li').data('item-id', item.id); 
$('#children ul').append($tpl); 
}); 
+1

你不增加的数据'$ tpl',你将它添加到您通过解析'$ tpl'创建的断开连接的节点。然后,当你将它追加到DOM时,你再次解析'$ tpl'。 – Barmar

+0

你为什么叫'find'? '$ tpl'已经有你想要的了。 – osahyoun

回答