2013-04-27 61 views
0

使用template.find來定位特定模板實例內的DOM元素非常有用。但是當模板在不使用子模板的情況下通過一些標籤迭代{{#each}}會發生什麼?在流星中分離模板區域以方便DOM導航

<template name="top"> 
    {{#each items}} 
    <img src="{{src}}"> 
    <a href="{{url}}">Click me</a> 
    {{/each}} 
</template> 

Tempalte.events(
    'click a': (event, template) -> 
    template.find('img')   # This doesn't do the trick 
            # Is there a better way? 
) 

有沒有一種方法可以方便地訪問與點擊事件相關聯的img元素?

我知道我可以使用event.target或創建另一個模板在{{#each}}塊中使用它。我想知道是否有更好/更短的方法來做到這一點。

回答

0

您可以添加一個@index到img,以便可以選擇它(使用js或CSS)。 How to get index in Handlebars each helper?

+1

似乎'@ index'尚未在Meteor的Handlebars實現中實現:https://github.com/meteor/meteor/issues/489#issuecomment-11270564 – Xyand 2013-04-27 16:37:06