为什么$.每个只显示一个我的小胡子模板

Why does $.each show only one of my Mustache templates?

本文关键字:我的 一个 小胡子 显示 为什么      更新时间:2023-09-26

我在$.each内部使用$.get来调用外部Mustache模板,以便在我的数组中的每个照片对象上使用。

下面是我的代码:
$.when.apply($, requests).then(function(dataOne, dataTwo) {
    $.each(dataOne, function(idx, obj) {
        $.get("myTemplate.mst", function(template) {
            var rendered = Mustache.render(template, obj);
            $photoElem.html(rendered);
        });
    });
});

我的问题是,一旦我刷新屏幕,只有一个我的数组对象显示。我知道{{#item}}要遍历数组,但我使用$.each是出于一个非常具体的原因。

谁能告诉我我做错了什么?

试试这个

 $.when.apply($, requests).then(function(dataOne, dataTwo) {
      $.get("myTemplate.mst", function(template) {
          $.each(dataOne, function(idx, obj) {
             var rendered = Mustache.render(template, obj);
             $photoElem.append(rendered);
         });
     }); 
 });

假设问题导致您替换$photoElem的内容(使用.html())而不是附加每个新项

我用。append(

)代替。html()

并更改代码为只读取模板一次(如@Andy说)

相关文章: