0
我在我的應用程序中有以下代碼。第一次觸發點擊事件模板將按預期添加到頁面中。Mustache.js複製templaer
但是,第二次添加模板的兩個副本,第三次添加三等。所以三次點擊後,我實際上有6個我的模板在DOM內的副本。
即使我將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);
});