JQuery Live implementation in prototype

Element.implement({
    addLiveEvent: function(event, selector, fn){
        this.addEvent(event, function(e){
            var t = $(e.target);

            if (!t.match(selector)) return false;
                fn.apply(t, [e]);
        }.bindWithEvent(this, selector, fn));
    }
});

$(document.body).addLiveEvent('click', 'a', function(e){ alert('This is a live event'); });

The above code was executed in a similar question to implement .live behavior in Mootools. I read the question: Prototype equivalent for jQuery live function .

How to implement this in Prototype? Perhaps something that can be implemented as follows:

document.liveobserve('click', 'a', function(e){ alert('This is a live event');

Edited to make the question clear.

+3
source share
1 answer

The simplest (and possibly not the fastest or best) way looks something like this:

Element.live = function(evType, evSelector, evBlock) {
  var mySelector = evSelector;
  var myBlock = evBlock;
  this.observe(evType, function(ev) {
    if (ev.target.match(mySelector)) {
      evBlock(ev);
    }
  });
};

evSelector evBlock , ( ). evBlock , Prototype.

, "evType", , mouseMove/mouseOver, . FireBug, , - , .

EDIT:

0

Source: https://habr.com/ru/post/1735571/


All Articles