Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

javascript - 如何清除按钮单击侦听器上的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>   ask by Sabrina translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...