event.stopPropagation 和 event.preventDefault 有什么区别?
他们似乎在做同样的事情......
一个是现代的,一个是旧的?或者不同的浏览器支持它们?
当我自己处理事件(没有框架)时,我总是检查两者并执行两者(如果存在)。 (我也返回 false
,但我感觉它不适用于 node.addEventListener
附加的事件)。
那为什么两者都有呢?我应该继续检查两者吗?还是实际上有区别?
(我知道,有很多问题,但它们都是一样的=))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
stopPropagation
防止当前事件在捕获和冒泡阶段进一步传播。preventDefault
阻止浏览器对该事件执行的默认操作。示例
preventDefault
停止传播
使用
stopPropagation
时,仅调用button
的点击处理程序,而div
的点击处理程序 永远不会触发。就好像您使用
preventDefault
一样,只有浏览器的默认操作会停止,但 div 的点击处理程序仍然会触发。以下是 MDN 中有关 DOM 事件属性和方法的一些文档:
event.preventDefault()
event.returnValue
event.stopPropagation()
对于 IE9 和 FF,您可以只使用 PreventDefault &停止传播。
要支持 IE8 及更低版本,请将
stopPropagation
替换为cancelBubble
,并将preventDefault
替换为returnValue
stopPropagation
prevents further propagation of the current event in the capturing and bubbling phases.preventDefault
prevents the default action the browser makes on that event.Examples
preventDefault
stopPropagation
With
stopPropagation
, only thebutton
's click handler is called while thediv
's click handler never fires.Where as if you use
preventDefault
, only the browser's default action is stopped but the div's click handler still fires.Below are some docs on the DOM event properties and methods from MDN:
event.cancelBubble
event.preventDefault()
event.returnValue
event.stopPropagation()
For IE9 and FF you can just use preventDefault & stopPropagation.
To support IE8 and lower replace
stopPropagation
withcancelBubble
and replacepreventDefault
withreturnValue
术语
来自 quirksmode.org:
来自 w3.org 的接口
,用于事件捕获:
对于事件冒泡:
对于活动取消:
示例
在以下示例中,单击 Web 浏览器中的超链接将触发事件流(执行事件侦听器)和事件目标的默认操作(打开新选项卡)。
HTML:
JavaScript:
示例 1:输出结果
示例 2:将
stopPropagation()
添加到函数结果的输出中
事件侦听器阻止了事件。但是,它并没有阻止默认操作(打开新选项卡)。
示例 3:向函数添加
stopPropagation()
或函数
结果输出
这是因为两个事件侦听器都注册在同一事件目标上。事件侦听器阻止事件进一步向上传播。但是,它们并没有阻止默认操作(打开新选项卡)。
示例 4:将
preventDefault()
添加到任何函数,例如防止打开新标签。
Terminology
From quirksmode.org:
Interface
From w3.org, for event capture:
For event bubbling:
For event cancelation:
Examples
In the following examples, a click on the hyperlink in the web browser triggers the event's flow (the event listeners are executed) and the event target's default action (a new tab is opened).
HTML:
JavaScript:
Example 1: it results in the output
Example 2: adding
stopPropagation()
to the functionresults in the output
The event listener prevented further downward and upward propagation of the event. However it did not prevent the default action (a new tab opening).
Example 3: adding
stopPropagation()
to the functionor the function
results in the output
This is because both event listeners are registered on the same event target. The event listeners prevented further upward propagation of the event. However they did not prevent the default action (a new tab opening).
Example 4: adding
preventDefault()
to any function, for instanceprevents a new tab from opening.
return false;
return false;
当您调用它时,它会执行 3 件不同的事情:event.preventDefault()
– 它会停止浏览器的默认行为。event.stopPropagation()
– 它阻止事件在 DOM 中传播(或“冒泡”)。请注意,此行为与普通(非 jQuery)事件处理程序不同,其中值得注意的是,
return false
不会阻止事件冒泡。preventDefault();
preventDefault();
做了一件事:它阻止浏览器的默认行为。何时使用它们?
我们知道它们的作用,但何时使用它们?简而言之,这取决于您想要完成什么。如果您想“只是”阻止默认浏览器行为,请使用
preventDefault();
。使用return false;当您想要阻止默认浏览器行为并阻止事件传播 DOM 时。在大多数情况下,您会使用 return false;你真正想要的是preventDefault()
。示例:
让我们尝试通过示例来理解:
我们将看到纯 JAVASCRIPT 示例
示例 1:
示例2:
示例3:
示例 4:
示例5:
情况:
将看到所有三个示例。
希望这些例子是清楚的。尝试在 html 文件中执行所有这些示例,看看它们是如何工作的。
return false;
return false;
does 3 separate things when you call it:event.preventDefault()
– It stops the browsers default behaviour.event.stopPropagation()
– It prevents the event from propagating (or “bubbling up”) the DOM.Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably,
return false
does not stop the event from bubbling up.preventDefault();
preventDefault();
does one thing: It stops the browsers default behaviour.When to use them?
We know what they do but when to use them? Simply it depends on what you want to accomplish. Use
preventDefault();
if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want ispreventDefault()
.Examples:
Let’s try to understand with examples:
We will see pure JAVASCRIPT example
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
cases:
Will see all three example.
Hope these examples are clear. Try executing all these examples in a html file to see how they work.
的引用。
这是此处 Event.preventDefault
PreventDefault 方法可防止事件执行其默认功能。例如,您可以在 A 元素上使用 PreventDefault 来阻止单击该元素离开当前页面:
当该元素的默认功能被阻塞时,该事件会继续在 DOM 中冒泡。
Event.stopPropagation
第二种方法 stopPropagation 允许事件的默认功能发生,但阻止事件传播:
stopPropagation 有效地阻止父元素了解其子元素上的给定事件。
This is the quote from here
Event.preventDefault
The preventDefault method prevents an event from carrying out its default functionality. For example, you would use preventDefault on an A element to stop clicking that element from leaving the current page:
While the element's default functionality is bricked, the event continues to bubble up the DOM.
Event.stopPropagation
The second method, stopPropagation, allows the event's default functionality to happen but prevents the event from propagating:
stopPropagation effectively stops parent elements from knowing about a given event on its child.
看看这个 真的很好& 4 分钟轻松阅读,其中包含上述文章中的示例。
Check out this really nice & easy 4 min read with examples from where the above piece was taken.
event.preventDefault();
阻止元素的默认操作发生。event.stopPropagation();
防止事件在 DOM 树中向上冒泡,从而防止任何父处理程序收到该事件的通知。例如,如果在
DIV
或FORM
内部附加了 click 方法的链接也附加了 click 方法,则它将阻止DIV< /code> 或
FORM
点击方法触发。event.preventDefault();
Stops the default action of an element from happening.event.stopPropagation();
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.For example, if there is a link with a click method attached inside of a
DIV
orFORM
that also has a click method attached, it will prevent theDIV
orFORM
click method from firing.Event.preventDefault - 停止浏览器默认行为。现在什么是浏览器默认行为。假设您有一个锚标记,它有一个 href 属性,并且该锚标记嵌套在一个有单击事件的 div 标记内。锚标记的默认行为是当单击锚标记时它应该导航,但 event.preventDefault 的作用是在这种情况下停止导航。但它永远不会停止事件的冒泡或事件的升级,即
结果将是
“元素被单击”
“容器被单击”
现在 event.StopPropation 它停止事件的冒泡或事件升级。现在,上面的示例
结果将是
“元素被单击”
有关更多信息,请参阅此链接
https://codeplanet.io/preventdefault-vs-stoppropagation-vs-stopimmediatepropagation/
Event.preventDefault- stops browser default behaviour. Now comes what is browser default behaviour. Assume you have a anchor tag and it has got a href attribute and this anchor tag is nested inside a div tag which has got a click event. Default behaviour of anchor tag is when clicked on the anchor tag it should navigate, but what event.preventDefault does is it stops the navigation in this case. But it never stops the bubbling of event or escalation of event i.e
The result will be
"element was clicked"
"container was clicked"
Now event.StopPropation it stops bubbling of event or escalation of event. Now with above example
Result will be
"element was clicked"
For more info refer this link
https://codeplanet.io/preventdefault-vs-stoppropagation-vs-stopimmediatepropagation/
让我们比较一下
preventDefault
和stopPropagation
所有这些都可以在事件处理函数中使用。
然而,
Lets compare
preventDefault
andstopPropagation
All of these can be used in event handler functions.
Whereas,