Prototype equivalent for jQuery live function

前端 未结 2 1347
遥遥无期
遥遥无期 2020-12-01 04:01

I need to bind event listener to all dynamicaly created elements by given css selector.

In jQuery, that would be

$(\".foo\").live(\"click\", function         


        
2条回答
  •  时光取名叫无心
    2020-12-01 04:31

    The correct answer to the question is here: http://gurde.com/2011/08/jquery-live-in-prototype/

    The equivalent of the jQuery .live() in Prototype is the Event.on() method:

    var handler = document.on(
        'click',
        'div[id^="post-"] .attached-post-thumbnail',
        function(event, element) {
            console.log(this);
            console.log(element);
        }.bind(this)
    );
    
    handler.stop();
    handler.start();
    

    Within the callback, the this keyword will always refer to the original element (in this case document).

提交回复
热议问题