JQuery:如何将事件附加到模板对象?

Ric*_*Lai 1 jquery templates

我有一个生成图像的模板,我将它绑定到div.

<script id="postTemplate" type="text/html">
<div class="post_1">
    <div class="postImage"><img src="${ImageUrl}" alt="Image"></div>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)

然后我绑定数据

<script>
    $("#postTemplate").tmpl(clientData).appendTo("#imagesArea");
</script>
Run Code Online (Sandbox Code Playgroud)

现在,我想要的是为刚刚创建的事件添加一个事件处理程序.就像是

("template img").error(function() {});
Run Code Online (Sandbox Code Playgroud)

向click之类的东西添加处理程序似乎有效,但是在我可以添加处理程序之前就会触发错误.

Cha*_*ant 5

不确定你的选择器"模板img"是什么引用,因为我没有看到任何模板元素.

$(function(){

    $('template img').live({
      click: function() {
        // do something on click
      },
      error: function() {
        // do something on error
      }
    });
});
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/live/