Javascript 在Handlebar模板中访问DOM元素
这一定比我想象的要简单。不知道发生了什么事 我有一个DIV,我正在用把手模板“填充”。生成模板后,我使用jQuery向下滑动来打开面板以查看内容。现在我需要设置一个关闭函数来滑动DIV 我认为问题在于click函数没有被绑定,因为a.close元素在脚本标记中 以下是内容的DIV:Javascript 在Handlebar模板中访问DOM元素,javascript,jquery,handlebars.js,Javascript,Jquery,Handlebars.js,这一定比我想象的要简单。不知道发生了什么事 我有一个DIV,我正在用把手模板“填充”。生成模板后,我使用jQuery向下滑动来打开面板以查看内容。现在我需要设置一个关闭函数来滑动DIV 我认为问题在于click函数没有被绑定,因为a.close元素在脚本标记中 以下是内容的DIV: <div id="characteristic" style="bottom:0px; width:800px; display:none; position:fixed; left: 350px;">&
<div id="characteristic" style="bottom:0px; width:800px; display:none; position:fixed; left: 350px;"></div>
以及模板的一个片段:
<script id="ac-template" type="text/x-handlebars-template">
<div class="holder" style="background-color:#FFFFFF;">
<div class="frame">
<div class="content">
<div class="info-box-holder">
<a class="close" href="">×</a>
<div class="heading">
<h2>ACTIONABLE CHARACTERISTIC</h2>
</div>
<div class="info-box">
<a href="#"><img class="alignleft" src="{{image_large}}" alt="" width="400" height="400" /></a>
{{#if subcategory_name}}
<h2>{{subcategory_name}}: {{name}}</h2>
{{else}}
<h2>{{category_name}}: {{name}}</h2>
{{/if}}
可诉性
{{{#如果子类别_name}
{{subcategory_name}}:{{name}
{{else}
{{category_name}}:{{name}
{{/if}
我知道这是一个老问题,您可能已经找到了答案,但是是的,这是因为在运行JS代码时,a.close
在DOM中不存在
您需要在Handlebar完成呈现模板后运行JS代码,或者绑定到页面加载中存在的更高级别DOM元素(某种容器),然后仅为所需的链接激活。类似这样的内容():
特征ID在哪里?可能需要在呈现模板后运行ready函数中的代码,因为在渲染过程中可能不存在控件onload@Amberlamps我已经为DIV添加了代码。
<script id="ac-template" type="text/x-handlebars-template">
<div class="holder" style="background-color:#FFFFFF;">
<div class="frame">
<div class="content">
<div class="info-box-holder">
<a class="close" href="">×</a>
<div class="heading">
<h2>ACTIONABLE CHARACTERISTIC</h2>
</div>
<div class="info-box">
<a href="#"><img class="alignleft" src="{{image_large}}" alt="" width="400" height="400" /></a>
{{#if subcategory_name}}
<h2>{{subcategory_name}}: {{name}}</h2>
{{else}}
<h2>{{category_name}}: {{name}}</h2>
{{/if}}
$(document).ready(function(e){
$("#mycontainerdiv").on('click', 'div.info-box-holder a.close', function(e) {
e.preventDefault();
$("#characteristic").slideUp();
});
});