我有一个循环:jQuery的追加()对循环之后的多个元素无需压缩到HTML
for (index = 0; index < total_groups; index += 1) {
groups[index].list_item = $(list_item_snippet);
// Closure to bind the index for event handling
(function (new_index) {
groups[index].list_item.find('.listing-group-title')
.html(groups[index].Group.Name)
.click(function(e){
fns.manageActiveGroup(new_index, groups);
return false;
});
})(index);
// Append to DOM
mkp.$group_listing.append(groups[index].list_item);
};
我宁愿不叫append()
每次循环火灾。
我知道我可以使用一个字符串并将标记与每个循环迭代连接起来,并在末尾附加字符串mkp.$group_listing
,但这会使对象变平并且绑定丢失(我不依赖于ID)。
有没有办法将我的对象添加到数组中,并将它们全部追加到底部,而不会翻译成HTML?
假设:
$(list_item_snippet)
包含一些HTML定义一个列表项(并且包括具有.listing-group-title
类的元素)。groups
是JSON在我的脚本定义一个“基团”一个块- 封闭工作完全
编辑:
发现我可以使用以下句法来追加多个元素:
mkp.$group_listing.append(groups[0].list_item, groups[1].list_item);
但我显然需要自动化它 - 它不是一个数组它只是可选的附加函数参数,所以我不知道该怎么做秒。
你如何约束事件?在'manageActiveGroup(...)里面''? – techfoobar
在代码片段中向右滚动;) - 对不起,应该重新格式化。 – Alex
@Alexcoady你需要看到我的答案 - 它解决了你的编辑问题。 – Alnitak