我想知道如何使用把手迭代以下数据对象。
这是它的输出:
image2.png | title2
image3.png | title3
我想达到的目标:
image1.png | title1
image2.png | title2
image3.png | title3
数据
var data = {
"item": [{ // item with one image
"src" : "image1.png",
"title" : "title1"
}],
"item": [{ // item with two or more images
"src" : "image2.png",
"title" : "title2"
},
{
"src" : "image3.png",
"title" : "title3"
}]
}
var template = Handlebars.compile($("#data-template").text());
var html = template(data);
$('#placeholder').html(html);
模板
<div id="placeholder"></div>
<script type="text/x-handlebars" id="data-template">
{{#item}}
{{src}} | {{title}} <br>
{{/item}}
</script>