给定 JSON data/css.json 这样的:
{ "CSS": [
{ "group":"Boxes", "css-class":"img", "syntaxe": "img/css-metric.png", "logic-english": "", "level":"A1" },
{ "group":"Boxes", "css-class":"list", "syntaxe": "margin", "logic-english": "", "level":"A1" },
{ "group":"Boxes", "css-class":"list", "syntaxe": "margin-top", "logic-english": "", "level":"B2" },
{ "group":"Boxes", "css-class":"list", "syntaxe": "margin-right", "logic-english": "", "level":"B2" }
]}
HTML 例如:
<body id="anchor"></body>
HTML 的 JS 如:
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script id="tpl" type="text/template">
{{#CSS}}<div class="{{css-class}}"><p class="{{level}} level">{{syntaxe}}</p><p class="logic-english">{{logic-english}}</p></div>{{/CSS}}
</script>
<script>// Code 4: WORKS
$(function() {
$.getJSON('data/css.json', function(data) {
var template = $('#tpl').html();
var info = Mustache.to_html(template, data);
$('#anchor').html(info);
});
});
</script>
通过编辑#tpl(我猜),如何根据以下值创建条件:
如果 css-class 的值 == "img" 然后注入:
<img src="{{syntaxe}}" alt="An image" height="64" width="64">
否则注入:
{{syntaxe}}
?