js中preventDefault 与stopPropagation 及 stopImmediatePropagation的区别
来源:互联网 发布:雷电网络 价格 编辑:程序博客网 时间:2024/06/01 23:49
原文:https://codeplanet.io/preventdefault-vs-stoppropagation-vs-stopimmediatepropagation/
Three similar and often confused methods. Let’s explore the different between:
- Event.preventDefault()
- Event.stopPropagation()
- Event.stopImmediatePropagation()
Summary
First off, let’s check out the MDN Summaries.
- preventDefault: Cancels the event if it is cancelable, without stopping further propagation of the event.
- stopPropagation: Prevents further propagation of the current event.
- stopImmediatePropagation: Prevents other listeners of the same event from being called.
Event.preventDefault
Let’s look at a code example. We know that clicking the submit button on a form submits it to the form handler. Event.preventDefault is a perfect way to not submit a form when the submit button is clicked.
Event.preventDefault will ensure that the form is never submitted, but it won’t control or prevent that submit or click event from bubbling up. That’s what the other two are for.
Event.stopPropagation
stopPropagation ensures that the event doesn’t bubble any further. Let’s look at another code example:
Now if you were to click the link with the console open, you would see:
Now let’s add Event.stopPropagation:
And click the link again. This time we see:
Event.stopImmediatePropagation
This gets you 90% of everything you’ll need as far as manipulating events. But let’s pose a last use case that can prove difficult.
We’ll start off with similar markup, except we’ll give the anchor two classes. A generic one, item, that all anchors in this area will get, and a specific one, element, that’s very important for our application to work.
And we’ll add our nifty Event.stopPropagation we learned about in the last section!
But, when we click on the element we see:
The problem here is that item and element are evenly ranked in the DOM. It’s not as though it hits element and then bubbles up to container like we saw in the last example. Since the click event fires on both element and item at the same time, you cannot stopPropagation like you’d expect.
This is where stopImmediatePropagation comes in handy!
Now one important thing to note here is: Battles over immediate propagation go to the first one declared in your script ( or series of scripts ). So as you may have noticed, in order to get stopImmediatePropagation working we had to switch the listeners so that element comes before item!
Let’s run it one last time and we should see:
- js中preventDefault 与stopPropagation 及 stopImmediatePropagation的区别
- js中preventDefault和stopPropagation两个方法的区别
- js中preventDefault和stopPropagation两个方法的区别
- js中preventDefault和stopPropagation两个方法的区别
- 怎样区别stopPropagation()与stopImmediatePropagation()
- jsz中event.preventDefault()与event.stopPropagation()的区别
- stopImmediatePropagation 和stopPropagation的区别
- e.preventDefault()与e.stopPropagation()的区别
- Flex事件流中有关 【 stopImmediatePropagation()和stopPropagation()】 的区别
- W3C/JQuery中stopImmediatePropagation和stopPropagation的区别
- jQuery 事件中stoppropagation和stopimmediatepropagation的区别
- javascript中stopImmediatePropagation函数和stopPropagation函数的区别
- js event preventDefault 与 stopPropagation
- js中preventDefault和stopPropagation
- Event preventDefault()与stopPropagation()区别
- js事件之event.preventDefault()与event.stopPropagation()用法区别
- js事件之event.preventDefault()与event.stopPropagation()用法区别
- js事件之event.preventDefault()与event.stopPropagation()用法区别
- bug : Undefined symbols for architecture x86_64 个案分析
- Codeforces 851D Arpa and a list of numbers (枚举+分段)
- jmeter与jenkins的集成
- 接口
- ==和equals http://www.cnblogs.com/dolphin0520/p/3592500.html
- js中preventDefault 与stopPropagation 及 stopImmediatePropagation的区别
- 带权路径长度 层次遍历
- 三个div,一个左浮动,一个有浮动,另外一个不浮动引发的样式问题
- Windows Practice(七)_MFC
- 网络层
- 菜鸟学习微信小程序之window
- Spring创建Bean的三种方式
- 「网络流 24 题」最长递增子序列
- 交换机、路由器、网关、集线器的区别