Google Closure event delegation a'la jQuery live/on

雨燕双飞 提交于 2019-12-22 08:18:17

问题


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

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

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

I would prefer something like

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

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.

Could anyone advice? I am new to Closures.

Thanks


回答1:


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);
    }
});

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

  • event name/type
  • the element where event has to bind
  • the action what has to be triggered on that particular event.


来源:https://stackoverflow.com/questions/11518215/google-closure-event-delegation-ala-jquery-live-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!