2011-12-08 107 views
2

如何将事件添加到由AJAX加载的元素。将事件添加到动态生成的元素

jQuery有live,但Mootools的等价物是什么?

例如:等效

window.addEvent('domready', function() { 
    // some code which loads new elements by ajax 

    // Filter 
    $$('.filterButton').addEvent('click', function(event) { 
     alert('wow'); 
    }); 
}); 
+0

可能重复的[如何在mootools上实现jQuery活动绑定事件?](http://stackoverflow.com/questions/2107892/how-to-implement-a-jquery-live-bind-event-on- mootools的)。在发布问题之前您是否在寻找?将“mootools jquery live live”这个单词放到google的搜索框中,马上给出答案。毫不奇怪,它在StackOverflow上:-)我的意思是大约需要10秒钟的时间,如果你有打开和搜索谷歌的捷径,可能会更少。 –

+0

这个答案已经过时了,所以他也问过。 –

回答

7

的MooTools是经由delegatorElement.addEvent("event:relay(mask)", fn);其中event可以是标准的鼓泡(点击,鼠标悬停/缩小/离开/进入等)或无泡(模糊,聚焦,改变等) 。

$(document.body).addEvent("click:relay(.filterButton)", function(event, element) { 
    console.log(event, element === this); // event obj, true 
}); 

这是更好地将事件添加到一个元素越往上DOM,像document.id("delegatorId")

更多在这里:

http://mootools.net/docs/core/Element/Element.Delegation

在你的代码,去为

请记住,事件代表团从1.4开始就使用mootools-core--它曾经是mootools--在此之前更多,这意味着一个习惯建立。

相关问题