Javascript 什么';这个handlebar.js代码有什么问题?
我有一个index.html,其中有这样的代码:我从Handlebar网站上的文档中遵循了它,但我无法使它工作。当我将字符串传递给Javascript 什么';这个handlebar.js代码有什么问题?,javascript,handlebars.js,Javascript,Handlebars.js,我有一个index.html,其中有这样的代码:我从Handlebar网站上的文档中遵循了它,但我无法使它工作。当我将字符串传递给var source时,它工作正常,但当我尝试使用$(#elem_id)获取模板时,Handlebar不会将数据插入其中 <html> <head> <script src="../static/js/handlebars-1.0.0.beta.6.js"></script> <script src="../sta
var source
时,它工作正常,但当我尝试使用$(#elem_id)
获取模板时,Handlebar不会将数据插入其中
<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>
{{title}}
{{body}}
$(文档).ready(函数(){
var source=$(“#条目模板”).html();
var template=handlebar.compile(源代码);
var d={“title”:“我的新帖子”,“body”:“这是我的第一篇帖子!”};
var结果=模板(d);
console.log(result);//这只是返回模板html,没有数据
});
我认为这是由于HTML位于脚本
标记中造成的。尝试将Html包装在不可见的div中(或使div本身不可见),例如:
{{title}}
{{body}}
我认为这是由于HTML位于脚本
标记中造成的。尝试将Html包装在不可见的div中(或使div本身不可见),例如:
{{title}}
{{body}}
我刚刚测试了它,它在我的机器上运行良好(使用Chrome)-你是在看旧版本还是什么?这很奇怪,它不适合我。这取决于某种jquery版本吗?你是对的,它是有效的:我的env有问题(我有大量的twitter引导代码)我刚刚测试过它,它在我的机器上运行良好(使用Chrome)-你是在看旧版本还是什么?这很奇怪,它对我不起作用。这是否取决于某种jquery版本?你是对的,它可以工作:我的env有问题(我有大量的twitter引导代码)请检查把手文档,它应该在标签中:请检查把手文档,它应该在标签中:
<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>