简单事件绑定(Jquery& Rails)

时间:2015-09-02 20:09:24

标签: jquery

我的DOM .customers-container中有一个容器。在该容器中,我通过ajax调用添加新的" tr" s。

如何将事件绑定应用于那些动态创建的元素。我在下面使用,但它没有处理新的元素:

 $('.customers-container').find('.glyphicon-remove').on('click', function(){
        $(this).closest('tr').hide();
     });

1 个答案:

答案 0 :(得分:2)

将活动从.customers-container委派给.glyphicon-remove

$('.customers-container').on('click', '.glyphicon-remove', function(){
    $(this).closest('tr').hide();
});

请注意,on方法参数已更改,现在第一个参数click是事件名称,第二个参数.glyphicon-remove是将从{接收事件绑定的元素{1}}委托人,第三个函数是事件触发时执行的函数。