2013-12-23 24 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

回答