如何阻止孩子传播由实时/委托侦听器触发的事件?

发布于 2025-01-02 21:45:50 字数 869 浏览 1 评论 0原文

我有一个委托父级,用于侦听具有特定类别的一组子级中的单击事件。

$(".toggle_group").on("click",".toggle",function(e){ .. });

所以这里是 html 的一个示例,

<div class='toggle_group'>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
</div>

问题是 a.toggle 的子级将事件传播给父级,在不应该触发处理程序的情况下触发处理程序。根据 jQuery 文档,您无法停止实时/委托事件侦听器上的事件传播。

如何阻止 a.toggle 的这些子子级将事件传播给父级?或者在某些情况下可能允许这样做?

您会看到 .toggle 的内部 div 应该是一个下拉菜单。当您单击菜单时,它不应该切换,只有当您单击切换链接时...

I have a delegation parent that listen for click events in a set of children with a specific class.

$(".toggle_group").on("click",".toggle",function(e){ .. });

so heres an example of the html

<div class='toggle_group'>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
  <a class='toggle'>click me and i toggle <div>Im a child of toggle</div></a>
</div>

the problem is that children of a.toggle propogate the event to the parent triggering the handler when it shouldn't. According to jQuery docs you cant stop event propagation on live/delegated event listeners.

How do I stop these sub children of a.toggle from propogating the event to the parent? or maybe in some cases permit it?

You see the inner div of .toggle is supposed to be a drop down menu. It shouldnt toggle when you click the menu, only when you click the toggle link...

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

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

发布评论

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

评论(2

扛起拖把扫天下 2025-01-09 21:45:50

检查此页面中的评论,至少有一些声称找到了解决方法。 http://api.jquery.com/event.stopPropagation/

从此处复制了线程的评论备查:

迈克W:
解决 .live() 事件传播问题。已创建节点
动态地不会收到事件,直到他们的父母已经
收到了。这会导致时髦并停止传播
PreventDefault 无法解决此问题。要修复:添加行

if(event.target != this){ return true; }

到父节点事件处理程序的顶部。这将阻止
当事件实际上被发送到 a 时,父事件不会被触发
子节点,并且(与 return false 不同)会将事件传播到
预期节点。

Check the comments in this page, at least some claim to have found workaround. http://api.jquery.com/event.stopPropagation/

Copied the comment from thread here for future reference:

MikeW:
work around for .live() event propagation issues. nodes created
dynamically will not receive the event until after their parent has
received it. this will cause funkyness and stopPropagation and
preventDefault will not solve this issue. To Fix: add the lines

if(event.target != this){ return true; }

to the top of the parent nodes event handler. this will stop the
parent event from firing when the event is actually addressed to a
child node, and (unlike return false) will propagate the event to the
intended node.

紫南 2025-01-09 21:45:50

另一种方法是仅当事件来自具有 .toggle 类(或其子级之一)的元素(而不是 .toggle_group 元素本身)时才执行函数处理程序。

$(".toggle_group").on("click",".toggle",function(e){
    if (!$(e.target).is(".toggle")) return;

    ...
});

Another approach is to only execute your function handler if the event comes from an element with the .toggle class (or one of its children), not the .toggle_group element itself.

$(".toggle_group").on("click",".toggle",function(e){
    if (!$(e.target).is(".toggle")) return;

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