我是 underscore.js 的新手,我希望以下内容可以呈现传递给它的列表,但它会呈现:
Title: {{title}}
Author: {{author}}
这是一个片段:
<script src="/Scripts/underscore.js"></script>
<script id="tmpl-books" type="text/template">
<ul>
<li>Title: {{title}}</li>
<li>Author: {{author}}</li>
</ul>
</script>
<script type="text/javascript">
var list =
[
{
"title": "Myst: The Book of Atrus",
"author": "Rand Miller"
},
{
"title": "The Hobbit",
"author": "J.R.R. Tolkien"
},
{
"title": "Stardust",
"author": "Neil Gaiman"
}
];
$(document).ready(function () {
var tmplMarkup = $('#tmpl-books').html();
var compiledTmpl = _.template(tmplMarkup, list);
$('#rendered').html(compiledTmpl);
});
</script>
在 Html 正文中,我有一个 id="rendered" 的 div
<html>
<body>
<div id="rendered"></div>
</body>
</html>