这个handlebars.js代码有什么问题? [英] What's wrong with this handlebars.js code?
本文介绍了这个handlebars.js代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个index.html,我有这个代码:我跟着它从handlebars网站的文档中,但我无法使它工作。当我将字符串传递给 var source
时,它工作正常,但是当我尝试使用 $(#elem_id)$ c
$ b
I have an index.html where I have this code: I followed it from the documentation on handlebars site, but I am not able to make it work. When I pass a string to var source
then it works fine, but when I try to get the template using $(#elem_id)
, handlebars does not insert the data in it.
<html>
<head>
<script src="../static/js/handlebars-1.0.0.beta.6.js"></script>
<script src="../static/jquery/1.7.1/jquery.js"></script>
</head>
<body>
<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
<script type="text/javascript">
$(document).ready(function() {
var source = $("#entry-template").html();
var template = Handlebars.compile(source);
var d = {"title": "My New Post", "body": "This is my first post!"};
var result = template(d);
console.log(result); //This just returns the template html, without the data
});
</script>
</body>
</html>
推荐答案
我认为这是由于您的HTML位于 script
标签。尝试将Html包装在一个隐形div内(或者让div本身不可见),即:
I assume this is caused by your HTML being in script
tags. Try wrapping your Html inside an invisible div (or make the div itself invisible), i.e.:
<div style='display:none;' id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</div>
这篇关于这个handlebars.js代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文