使用jQuery从Handlebar模板获取每个元素
我想从模板中获取每个项目。该模板如下所示:使用jQuery从Handlebar模板获取每个元素,jquery,handlebars.js,Jquery,Handlebars.js,我想从模板中获取每个项目。该模板如下所示: <div class="row flush" id="photos_list"> <script id="photo_list_template" type="text/x-handlebars-template"> {{#each this}} <div class="3u photo">
<div class="row flush" id="photos_list">
<script id="photo_list_template" type="text/x-handlebars-template">
{{#each this}}
<div class="3u photo">
<img src="{{url}}" alt="{{name}}"/>
</div>
{{/each}}
</script>
</div>
这是生成的html:
<div class="row flush" id="photos_list">
<div class="3u photo">
<img src="http://everythingawesomeever.files.wordpress.com/2013/07/awesome-meter.jpg" alt="Again, with the insanity.">
</div>
<div class="3u photo">
<img src="http://who-is-awesome.com/who-is-awesome.jpg" alt="Who's awesome?">
</div>
<div class="3u photo">
<img src="http://www.miataturbo.net/attachments/insert-bs-here-4/78009-my-little-random-picture-thread-*sfw-huffy-*-1682345-slide-slide-1-biz-stone-explains-how-he-turned-91-random-photos-into-movie-jpg?datelin" alt="After Mask">
</div>
<div class="3u photo">
<img src="http://www.miataturbo.net/attachments/insert-bs-here-4/76756-my-little-random-picture-thread-*sfw-huffy-*-11254201pkm5958-jpg?dateline=1368653578" alt="English Muffin">
</div>
</div>
我该怎么做?这应该可以做到,因为在绑定事件时,img标记不在dom中,您需要以父对象为目标,并选择事件必须应用于其中的哪个元素:
$('div.row').on("mouseenter", "img", function () {
// some code
});
$('div.row').on("mouseleave", "img", function () {
// some code
});
这应该可以做到,因为在绑定事件时,img标记不在dom中,您需要以父对象为目标,并选择事件必须应用于其中的哪个元素:
$('div.row').on("mouseenter", "img", function () {
// some code
});
$('div.row').on("mouseleave", "img", function () {
// some code
});
如果生成dom,则应使用此选项:
$("div.row").on("mouseenter mouseleave", "img", function(e){
if(e.type == "mouseenter"){
..
}else{
...
}
});
如果生成dom,则应使用此选项:
$("div.row").on("mouseenter mouseleave", "img", function(e){
if(e.type == "mouseenter"){
..
}else{
...
}
});
似乎是对的,发布生成的html。。也许有一些问题。。。另外,悬停为两种状态都使用了2个函数(){}…似乎是正确的,发布生成的html。。也许有一些问题。。。另外,悬停在两种状态下都需要2个函数(){}…如果我用“点击”而不是“悬停”,它可以工作,但在“悬停”时它什么都不做。如果我用鼠标指针,那么我必须用鼠标指针进入前一阶段,不?如果我用“点击”而不是“悬停”,它可以工作,但在“悬停”时它什么都不做。如果我用鼠标指针,然后我必须用鼠标才能进入前一阶段,不是吗?