mootools 使用 Event.Delegation (:relay) 时停止事件冒泡

发布于 2024-12-11 18:07:23 字数 825 浏览 0 评论 0原文

我正在向通过 ajax 加载的链接添加事件。以下是 html 的简化示例:

<div class="ajax-content">
  <div class="parent">
    <a href="#" class="event-link">Click here</a>
  </div>
</div>

“.ajax-content”中的所有内容均通过 ajax 加载。我不希望“.event-link”的点击事件冒泡到“.parent”。如果它不是通过 ajax 加载的,我可以这样做:

$$('.event-link').addEvent('click', function(event){
  event.stopPropagation();
  event.preventDefault();
  //do stuff
});

但是,我不知道如何使用 (click:relay) 来完成相同的行为。这就是我正在尝试的:

$$('.ajax-content').addEvent('click:relay(.event-link)', function(event){
  event.stopPropagation();
  event.preventDefault();
  //do stuff
});

How do I Prevent the click event from fire on '.parent'? (我还尝试了其他语法 - event.stop() 并返回 false,但它们具有相同的结果。)

I am adding events to links that are loaded via ajax. Here is a simplified example of the html:

<div class="ajax-content">
  <div class="parent">
    <a href="#" class="event-link">Click here</a>
  </div>
</div>

All content within ".ajax-content" is loaded via ajax. I don't want the click event for ".event-link" to bubble up to ".parent". If it was not loaded via ajax I could do this:

$('.event-link').addEvent('click', function(event){
  event.stopPropagation();
  event.preventDefault();
  //do stuff
});

However, I can't figure out how to accomplish the same behavior with (click:relay). This is what I am trying:

$('.ajax-content').addEvent('click:relay(.event-link)', function(event){
  event.stopPropagation();
  event.preventDefault();
  //do stuff
});

How do I prevent the click event from firing on '.parent'? (I also tried other syntax - event.stop() and returning false, but they had the same results.)

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

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

发布评论

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

评论(2

ゃ懵逼小萝莉 2024-12-18 18:07:23

考虑您的 DOM 顺序以及从 DOM 树顶部向下的事件传播顺序:

<div class="ajax-content">
  <div class="parent">
    <a href="#" class="event-link">Click here</a>
  </div>
</div>

您将事件附加到 div.ajax-content (委托者)。这意味着,当它击中它时,它已经需要通过 a.event-link 冒泡,然后是 div.parent,最后到 div.ajax-内容。那么停止它是没有意义的,div.parent 无论如何都会先出现:

http://jsfiddle.net/ dimitar/ChrCq/

这是当您在 .parent 中的事件到达委托人之前停止该事件时会发生的情况:

http://jsfiddle.net/dimitar/ChrCq/1/

委托并非没有恐怕还有缺点:)

您可能还想阅读最近的这个问题/答案,其中涉及委托事件的更多注意事项以及它们与正常事件的不同之处,如何通过交替委托者来解决困难。 制作(可能动态加载)元素可通过 JavaScript 点击,但优先考虑 中包含的链接 - 我仍然希望 mootools 中的事件委托可能会有所改变,但我只是不明白它是如何改变的会对你的情况有所帮助。

consider your DOM order and order of event propagation from the top of the DOM tree down:

<div class="ajax-content">
  <div class="parent">
    <a href="#" class="event-link">Click here</a>
  </div>
</div>

you attach the event to div.ajax-content (the delegator). which means, by the time that it hits it, it ALREADY needs to have bubbled through a.event-link, then div.parent and then finally down to div.ajax-content. there is no point in stopping it then, div.parent will come first anyway:

http://jsfiddle.net/dimitar/ChrCq/

here's what happens when you stop the event from the .parent before it reaches the delegator:

http://jsfiddle.net/dimitar/ChrCq/1/

delegation is not w/o it's downsides, i am afraid :)

you may want to also read this recent question / answer that went into some more caveats of delegated events and how they differ from normal events, how to work around the difficulties by alternating delegators. Make (possibly dynamically loaded) element clickable via JavaScript, but give precedence to links contained within - I am still hoping the event delegation in mootools may change somewhat but I just don't see how it will help things in your case.

复古式 2024-12-18 18:07:23

我试图阻止在 div.parent 上触发委托单击事件。在我的特定情况下,这可以通过在 div.parent 事件中添加对事件目标的检查来完成:

$('.ajax-content').addEvent('click:relay(.event-link)', function(event){
  console.log("hi");
  event.preventDefault();
  //do stuff
});

$('.ajax-content').addEvent('click:relay(.parent)', function(event) {
  if (event.target.match('a')) return false;
  console.log("parent");
});

感谢 Dimitar 为我指明了正确的方向!

I was trying to prevent a delegated click event from firing on div.parent. This can be accomplished in my particular situation by adding a check on the event target in the div.parent event:

$('.ajax-content').addEvent('click:relay(.event-link)', function(event){
  console.log("hi");
  event.preventDefault();
  //do stuff
});

$('.ajax-content').addEvent('click:relay(.parent)', function(event) {
  if (event.target.match('a')) return false;
  console.log("parent");
});

Thanks to Dimitar for pointing me in the right direction!

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