我有一個JSON數組對象,並試圖找出如何在Mustache.js中顯示它們。該數組的長度和內容可以是可變的。鬍鬚,遍歷JSON對象
例子:
[ Object { id="1219", 0="1219", title="Lovely Book ", url= "myurl} , Object { id ="1220" , 0="1220 , title "Lovely Book2" , url="myurl2"}]
我用盡:
$.getJSON('http://myjsonurl?type=json', function(data) {
var template = $('#personTpl').html();
var html = Mustache.to_html(template, data);
$('#test').html(html);
和模板:
<script id="personTpl" type="text/template">
TITLE: {{#data}} {{title}} IMAGE: {{image}}
<p>LINK: <a href="{{blogURL}}">{{type}}</a></p> {{/data}}
</script>
但不顯示任何內容。
我試圖把JSON到一個數組,然後訪問它直接使用products[1]
是這樣的:
$.getJSON("http://myjsonurl?type=json", function(json)
{
var products = [];
$.each(json, function(i, product)
{
var product =
{
Title:product.title,
Type:product.type,
Image:product.image
};
products.push(product);
;
});
var template = "<h1>Title: {{ Title }}</h1> Type: {{ Type }} Image : {{ Image }}";
var html = Mustache.to_html(template, products[1]);
$('#json').html(html);
});
這將顯示一條記錄很好,但我怎麼能在它們之間迭代並顯示所有?
這樣做是爲了循環,如果對象的數組是可變的內容不健全適合於模板 – maxbeatty 2012-03-15 07:05:37