我有一个具有以下结构的Json:
var data = {
"h": {
"results": {
"0": {
"title": "Do feral cats affect small animals?",
"authors": " Billie Theresa Lazenby, Billie Theresa Lazenby",
"url": "#",
"published": 2012,
"summary": ""
}
},
"categoryTitle": "Sydney eScholarship",
},
"j": {
"results": {
"0": {
"title": "Utagawa Kunisada II",
"description": "You can learn more ...",
"url": "#",
"thumb": "#",
"published": 2010
},
"1": {
"title": "Utagawa Kunisada II2",
"description": "You can learn more ...",
"url": "#",
"thumb": "#",
"published": 2012
}
},
"categoryTitle": "YouTube",
}
}
和js如下:
var source = $("#entry-template").html();
var template = Handlebars.compile(source);
var html = template(data);
$('#Content').html(html);
我需要首先访问data.h.categoryTitle和data.j.categoryTitle,然后访问data.h.results.Title和data.j.results [0] .Title和data.j.results [1] .Title作为嵌套迭代,这是我的模板:
<div id="content"> </div>
<script id="entry-template" type="text/x-handlebars-template">
{{#each data}}
<div class="searchResultItem col-sm-9 col-xs-12">
{{#each results}}
<a href="#" class="title">{{this.title}}</a>
{{/each}}
<span>{{this.categoryTitle}}</span>
</div>
{{/each}}
</script>
它什么也没显示:-| 我该如何使用把手?
非常感谢!
您在错误的情况下输入了错误的ID,脚本中的大写字母是字母“ C”,而html中的字母是small。因此无法找到要呈现生成的html的元素。这就是为什么什么都没出现的原因。
换线
var html = template(data);
$('#Content').html(html);
至
var html = template({data: data});
$('#content').html(html);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句