本文介绍了Google Closure事件委托a'la jQuery live / on的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将事件委托给新创建的元素,我需要为其创建事件附加处理程序。类似的东西:onCreate

I need to delegate event to newly created elements, I need to attach handler to their creation event. Something similar to: onCreate

我不想在创建后通过处理将事件绑定到元素:
jQuery:

I do not want to bind the event to the element after the creation by addressing it:jQuery:

$(element).click(function(){});

我喜欢

$.on('document','spawn', '.item', function(e) {
    if (e.target == this.target) {
        alert('element created: '+this);
    }
});

在google闭包中有办法吗?目标是将事件附加到创建,而不是调用函数附加它。

Is there a way to do so in google closure? the goal is to attach the event on creating and not calling function to attach it.

任何人都可以建议?

感谢

推荐答案

在jQuery类似的东西,但方法是不同的。例如:

So we have something similar in jQuery but the method is different. Here is the example:

$(document).on('spawn', '.item', function(e) {
    if (e.target == this.target) {
        alert('element created: '+this);
    }
});

因此, $(document) ,你可以有元素必须呈现的上下文, .on()中有三个参数:

So, here under $(document), you can have the context where the element has to present and .on() has three params in it:


  • 事件名称/类型

  • 事件必须绑定的元素


这篇关于Google Closure事件委托a'la jQuery live / on的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:59