我的应用程序中有以下代码。第一次触发 click 事件时,模板会按预期添加到页面中。
但是第二次添加了两个模板副本,第三次添加了三个等等。所以在点击三下后,我实际上在 DOM 中有 6 个模板副本。
即使我在添加后将 template_view 设置为 null,它也会继续发生。
有人可以解释为什么会发生这种情况以及如何避免它吗?
template_path = "/templates/";
var template_html;
$.get(template_path+"template.html", function(template) {
template_html = template;
});
$('#test_add').click(function(){
var template_data = {name:'whatever'};
template_view = Mustache.to_html(template_html, template_data);
$(body).append(template_view);
});