0

我的应用程序中有以下代码。第一次触发 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);
});
4

1 回答 1

0

仍然不确定为什么会这样。最初虽然我的模板是一个静态 html 文件,但其中没有小胡子标签。添加了一些并传递一个数组以循环遍历它现在不再在每次调用函数时都复制模板

于 2013-03-13T16:20:26.867 回答