如何在mootools上实现jQuery live bind事件?

loc*_*ock 3 javascript jquery mootools dom

如何制作通过ajax加载的元素,在mootools 1.11上采用与同一类相关的事件?

据我所知,在jQquery中,如果你的ajax响应包含类似的东西<div class='button'>,
如果有一个事件绑定使用liveto $('.button'),那些事件会自动绑定.

MooTools 1.11可以实现吗?

ano*_*reh 7

也许像这样的东西可能会做你想要的东西?虽然我不确定它是否适用于1.11.

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'); });
Run Code Online (Sandbox Code Playgroud)

  • 不要使用它,请使用http://mootools.net/docs/core/Element/Element.Delegation (4认同)