-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopImmediate.js
More file actions
51 lines (49 loc) · 1.63 KB
/
stopImmediate.js
File metadata and controls
51 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import stopEvent from './stopEvent'
/**
* 阻止监听同一事件的其他事件监听器被调用,并且阻止默认行为和事件冒泡。
* ========================================================================
* @method stopImmediate
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Event/stopImmediatePropagation
* @param {Event} evt - (必须)Event 对象
*
* @example
* <div id="nav" class="nav">
* <a id="service" class="anchor" href="https://www.yaohaixiao.com/serivce">Service</a>
* <a id="help" class="anchor" href="https://www.yaohaixiao.com/help">Help</a>
* </div>
*
* const $nav = document.querySelector('#nav')
* const $service = document.querySelector('#service')
* const logHandler = function(evt) {
* console.log(evt.target)
* }
* const styleHandler = function(evt) {
* $nav.classList.add('checked')
* }
* const serviceHandler = function(evt) {
* alert(evt.target)
* stopImmediate(evt)
* }
* const removeHandler = function(evt) {
* const $target = evt.target
*
* $target.parentNode.removeChild($target)
* }
*
* $nav.addEventListener('click', logHandler)
* $nav.addEventListener('click', styleHandler)
* $service.addEventListener('click', serviceHandler)
* $service.addEventListener('click', removeHandler)
*
* $nav.click()
* // => 触发 logHandler 和 styleHandler
*
* $service.click()
* // => 仅触发 serviceHandler,不会触发 removeHandler
* // => 并且不会跳转页面,也不会冒泡到 $nav,不会触发 logHandler 和 styleHandler
*/
const stopImmediate = function (evt) {
stopEvent(evt)
evt.stopImmediatePropagation()
}
export default stopImmediate