Google Closure event delegation a'la jQuery li

2019-05-07 07:07发布

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条回答
等我变得足够好
2楼-- · 2019-05-07 08:06

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.
查看更多
登录 后发表回答