我是handlehars的新手,感觉基础教程对新手不太友好。我必须将各个部分组合在一起,并且以下代码似乎不起作用。 html 已正确生成,这意味着它确实有效,但没有显示任何内容。
<!DOCTYPE html>
<html lang="en">
<head>
<title>test handlebars</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
</head>
<body>
<div class="container"></div>
<script>
$(document).ready(function(){
var source = $("#entry-template").html();
var template = Handlebars.compile(source);
var context = {title: "My New Post", body: "This is my first post!"};
var html = template(context);
console.log(html);
$(".container").innerHTML = html;
});
</script>
</body>
最佳答案
$(".container").innerHTML = html;
您正在混合普通 JS 和 jQuery。要么以普通方式设置 HTML,
document.querySelector(".container").innerHTML = html;
或者 jQuery 方式:
$(".container").html(html);
关于javascript - Handlebars : why this code doesn't work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529026/