简体   繁体   English

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

[英]how to implement a jQuery live bind event on mootools?

How do I make elements that are loaded via ajax, adopt the events associated with the same class on mootools 1.11? 如何制作通过ajax加载的元素,在mootools 1.11上采用与同一类相关的事件?

As far as I know, in jQquery, if your ajax response consists of something like <div class='button'> , 据我所知,在jQquery中,如果你的ajax响应包含<div class='button'>的东西,
if there is an event bind using live to $('.button') , those events would automatically bind. 如果有一个使用live$('.button')的事件绑定,那些事件将自动绑定。

Is that possible with MooTools 1.11? MooTools 1.11可以实现吗?

Perhaps something like this might do what you're looking for? 也许像这样的东西可能会做你想要的东西? Though I'm not sure if it'll work with 1.11. 虽然我不确定它是否适用于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'); });

You can use this way: 你可以这样使用:

$(document.body).addEvent('click:relay(.filterButton)', function(){
// do something
});

anomareh is on the right track. anomareh走在正确的轨道上。

You would also want to check the ancestor elements of the event target. 您还需要检查事件目标的祖先元素。

I'm not sure if this works with all events since some of them do not bubble (not sure how Mootools handles this). 我不确定这是否适用于所有事件,因为它们中的一些不会冒泡(不确定Mootools如何处理这个)。

This is very cool idea, jQuery .live() works in similar way, but there is also problem with bubbling . 这是非常酷的想法,jQuery .live()以类似的方式工作,但是冒泡也有问题。 If some parent has attached stopPropagation() for this event nothing happens. 如果某个父级已为此事件附加了stopPropagation() ,则不会发生任何事情。

I think the ideal solution is building custom events, here is very good post about custom events written by Nicholas Zakas: 我认为理想的解决方案是构建自定义事件,这里有关于Nicholas Zakas编写的自定义事件的非常好的帖子:

http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/ http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/

But this example doesn't have event bubbling implemented yet. 但是这个例子还没有实现事件冒泡。 Some kind of bubbling which has fallback for it's prevention should solve the problem. 某种鼓泡有其后备因素可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM