简体   繁体   English

如何清除按钮单击侦听器上的mustache.js模板以添加不同的数据

[英]how to clear mustache.js template on button click listener to add different data

I created a mustache template that contains different json parts and I append data to it dynamically json array, but what I want to do is clear the template part before adding new data to it 我创建了一个包含不同json部分的小胡子模板,并向其动态添加json数组数据,但是我想做的是在向其添加新数据之前清除模板部分

function loadTempalte(data){
    var template = $('#template').html();
            Mustache.parse(template);
            for ( oneResult of data) {
                var rendered = Mustache.render(template, {key1: oneResult.Auther.toString(),
                key2: oneResult.book.toString()});
                $('#result-block').append(rendered);
            }
        }
}
<script id="template" type="x-tmpl-mustache">
                     {{ author}}
                     <br>
                     {{book}}
                      <hr>
                </div>
</script>

try this 尝试这个

function loadTempalte(data){
    // new line
    $('#result-block').html("");
    var template = $('#template').html();
            Mustache.parse(template);
            for ( oneResult of data) {
                var rendered = Mustache.render(template, {key1: oneResult.Auther.toString(),
                key2: oneResult.book.toString()});
                $('#result-block').append(rendered);
            }
        }
}
<script id="template" type="x-tmpl-mustache">
                     {{ author}}
                     <br>
                     {{book}}
                      <hr>
                </div>
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM