将事件添加到动态生成的元素

发布于 2024-12-20 04:07:02 字数 321 浏览 2 评论 0原文

如何向 AJAX 加载的元素添加事件。

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

例如:

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

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

How can I add an event to an element which was loaded by AJAX.

jQuery has live, but what's the Mootools equivalent?

For example:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-12-27 04:07:02

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

请记住,事件委托位于 mootools-核心自1.4 - 在此之前它曾经位于 mootools-more 中,这意味着自定义构建。

the mootools equivalent is via delegatorElement.addEvent("event:relay(mask)", fn); where event can be standard bubbling (click, mouseover/out/leave/enter etc) or non-bubbling (blur, focus, change etc).

in your code that goes as:

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

it's better to add the event to an element higher up the dom, like document.id("delegatorId")

more here:

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

keep in mind, event delegation is in mootools-core since 1.4 - it used to be in mootools-more before that, which means a custom build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文