【发布时间】:2013-06-28 17:41:00
【问题描述】:
我对编程很陌生,尤其是对车把。我在我的 html 文件中加载了一个 div,其中包含 jquery 和车把的组合。我在它上面有标签。单击选项卡时,我想将所有内容重新加载到新内容。内容相似(结构相同),但标签、图像等必须更改。我尝试在handlebars.js 中使用部分。 这是一个示例代码。
<script type="text/x-handlebars-template" id="all-handlebar">
{{#each tabs}}
<div id=tab{{@index}}>{{this.text}}</div>
{{/each}}
<div id="tab-content">{{> tabContentPartial}}
</script>
<script type="text/x-handlebars-template" id="tab-content-partial">
<div id="mycontent">
<div id="text">{{mycontent.text}}</div>
<div id="text">{{mycontent.image}}</div>
</div>
</script>
<script type="text/javascript">
source=$("#all-handlebar").html();
var template = Handlebars.compile(source);
Handlebars.registerPartial("tabContentPartial", $("#tab-content-partial").html())
var context = {
mycontent : {
text="something that has to change everytime I click on a different tab",
image="idem"
};
var html = template(context);
$("body").html(html);
</script>
它第一次加载很好,但是当我单击选项卡时,我要求他重新注册选项卡内容部分脚本并且它不再存在,因为它已在我的代码中更改为 HTML 块。如何使用新内容重用和重新加载我的部分脚本?
非常感谢!
【问题讨论】:
-
如果您提供一些相关代码会很有帮助。通常你通过你的编译你的车把模板。然后你传递你的数据,它返回你然后插入到你的页面的创建 html。这个编译后的模板可以重复使用不同的数据。
-
现在清楚了吗?抱歉耽搁了,我尝试了不同的解决方案。
标签: jquery html handlebars.js partials