简体   繁体   English

e.stopPropogation()是否等效于Click事件的捕获阶段?

[英]e.stopPropogation() Equivalent for Capture Phase of Click Event?

I'm looking for a way in JS to stop a click event from continuing down the DOM to a nested child element (capture phase) when the parent element was clicked. 我正在寻找一种在JS中阻止单击事件的方法,以阻止单击事件在单击父元素时将DOM继续向下到嵌套的子元素(捕获阶段)。

It would be the inverse of the e.stopPropogation() function to prevent a click event from bubbling up. 这将是e.stopPropogation()函数的逆函数,以防止单击事件冒泡。

Is there a native JS function for this? 是否有本机JS函数?

Edit 03/10 Link to example 编辑03/10 链接到示例

Edit 03/11 Typo in the function call - it's stopPropagation() , not stopPropogation() . 在函数调用中编辑03/11 Typo-它是stopPropagation() ,而不是stopPropogation() Thanks to @JackPattishall for the find. 感谢@JackPattishall的发现。

yes there is native js function for capturing event in capture phase. 是的,在捕获阶段有用于捕获事件的本地js函数。

In all browsers, except IE<9, there are two stages of event processing. 在所有浏览器中,除了IE <9之外,都有两个阶段的事件处理。

The event first goes down - that's called capturing, and then bubbles up. 事件首先下降-称为捕获,然后上升。 This behavior is standartized in W3C specification. W3C规范中对此行为进行了规范化处理。

All methods of event handling ignore the caputiring phase. 所有事件处理方法都将忽略caputiring阶段。 Using addEventListener with last argument true is only the way to catch the event at capturing. 仅在最后一个参数为true的情况下使用addEventListener才是捕获事件时捕获事件的方法。

elem.addEventListener( type, handler, phase ) elem.addEventListener(type,handler,phase)

phase = true The handler is set on the capturing phase. phase = true处理程序在捕获阶段设置。 phase = false The handler is set on the bubbling phase. phase = false处理程序在冒泡阶段设置。

For better understanding of event capturing and bubbling you can follow this link 为了更好地了解事件捕获和冒泡,您可以点击此链接

okey, One thing you and do is use the stopPropogation() at the top in Dom. okey,您和您要做的一件事是使用Dom顶部的stopPropogation()。

eg. 例如。 you have a table(#table1) and elements(tr,td). 你有一张桌子(#table1)和元素(tr,td)。 so, if i do: 所以,如果我这样做:

table1.addEventListener("click",function(event){
  event.stopPropagation();
  console.log(this);
},true);
tableElem[2].addEventListener("click",function(event){
  console.log(this);
});
tableElem[2].addEventListener("click",function(event){
  console.log("hii");
});

The event will be intercepted at top and propogation will be stopped. 该事件将在顶部被拦截,传播将停止。 stopPropogation() stops propagation irrespective of on which step event is intercepted. stopPropogation()停止传播,无论在哪个步骤事件上被拦截。

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

相关问题 addEventListener 事件 Uncaught TypeError: e.stopPropogation is not a function - addEventListener event Uncaught TypeError: e.stopPropogation is not a function 单击时保持 wistia 覆盖层打开 - 外部框上的 e.stopPropogation 防止其关闭 - stopPropgation 误解 - keep wistia overlay open on click - e.stopPropogation on outside box preventing it from closing - stopPropgation misunderstanding 试图理解 e.PreventDefault 和 e.StopPropogation 行为 - Trying to understand e.PreventDefault and e.StopPropogation behavior e.PreventDefault()和e.stopPropogation()在平板电脑和移动设备上不起作用 - e.PreventDefault() & e.stopPropogation() not working on tablet & mobile 是否存在与'touchstart'事件相同的e.PageX位置,因为有点击事件? - Is there an equivalent to e.PageX position for 'touchstart' event as there is for click event? KnockoutJS:处理事件捕获阶段 - KnockoutJS: Handle event capture phase event.stopPropogation不是函数 - event.stopPropogation is not a function 如何在捕获阶段触发自定义事件 - How to trigger a custom event in capture phase 带有.on(&#39;click&#39;)绑定的jQuery stopPropogation无法正常工作 - JQuery stopPropogation with .on('click') binding not working event.stopPropogation 未按预期工作 - event.stopPropogation not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM