简体   繁体   English

click 事件总是在父元素上触发,尽管在子元素上调用了 stopPropation()

[英]click event always fires on parent element, despite stopPropation() having been called on child element

I have a seemingly trivial problem but cannot find the cause.我有一个看似微不足道的问题,但找不到原因。 There is a minimal html structure:有一个最小的 html 结构:

<div id="bg">
  <div id="panel"></div>
  <div id="helper"></div>
</div>

Element #helper has display:none, and on mousedown event on #panel I set dipslay:block on #helper.元素#helper 有display:none,并且在#panel 上的mousedown 事件中我在#helper 上设置了dipslay:block。

The problem can be described briefly like this: when I click inside element #panel, the click event always fires on element #bg, despite all effort to stop the event propagation on event handlers on #panel and #helper.这个问题可以简单描述如下:当我在元素#panel 内部单击时,单击事件总是在元素#bg 上触发,尽管所有努力都阻止了#panel 和#helper 上的事件处理程序上的事件传播。 I made a codepen to illustrate the problem:我做了一个codepen来说明这个问题:

https://codepen.io/tony124/pen/LYpQzwx?editors=1111 https://codepen.io/tony124/pen/LYpQzwx?editors=1111

when I click outside of #panel, I get in console:当我在#panel 之外单击时,我进入控制台:

"onMouseDownBg" "bg" "bg"
"onMouseUpBg" "bg" "bg"
"onClickBg" "bg" "bg"

which is to be expected.这是意料之中的。 However when I click on #panel, I get但是,当我单击#panel 时,我得到

"onMouseDownPanel (stop)" "panel" "panel"
"onMouseUpHelper (stop)" "helper" "helper"
"onClickBg" "bg" "bg"

which I cannot understand why.我不明白为什么。 There is even no logging from onMouseDownBg and noMouseUpBg.甚至没有来自 onMouseDownBg 和 noMouseUpBg 的日志记录。 How did the click event can ever fire? click 事件是如何触发的?

Because your onMouseDownPanel function makes <div id="helper"> as display block and as you release mouse button because helper was visible and due to positioning it is triggering onMouseUpHelper and finally as they are all children of bg it calls onClickBg因为您的onMouseDownPanel function 使<div id="helper">作为显示块并且当您释放鼠标按钮时,因为助手是可见的并且由于定位它正在触发onMouseUpHelper最后因为它们都是 bg 它调用onClickBg

It's about the order in which the events get called in and the actions taking place within the handlers.这是关于事件被调用的顺序和在处理程序中发生的动作。

  1. onMouseDown event: onMouseDown事件:
    • event is consumed & cancelled by #panel.事件被#panel 消费和取消。
    • #helper becomes active #helper 变得活跃
  2. onMouseUp event: onMouseUp事件:
    • event is consumed & cancelled by #helper.事件被#helper 消费和取消。
    • #helper becomes inactive #helper 变为非活动状态
  3. onClick event: onClick事件:
    • From MDN :来自MDN

      An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element .指针位于元素内部时,当同时按下和释放定点设备按钮(例如鼠标的主鼠标按钮)时,元素会接收到单击事件。 If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements.如果在一个元素上按下按钮并且在释放按钮之前将指针移到元素之外,则在包含这两个元素的最具体的祖先元素上触发事件。 (emphasis mine) (强调我的)

In this case, the most specific ancestor element would be #bg, thus it handles the click event.在这种情况下,最具体的祖先元素将是#bg,因此它处理点击事件。


To get the effect you're probably looking for, just move the activation of the #helper element to the onMouseUpPanel handler要获得您可能正在寻找的效果,只需将 #helper 元素的激活移动到onMouseUpPanel处理程序

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

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