-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopEvent.js
More file actions
37 lines (35 loc) · 1.11 KB
/
stopEvent.js
File metadata and controls
37 lines (35 loc) · 1.11 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
import stopPropagation from './stopPropagation'
import preventDefault from './preventDefault'
/**
* 停止事件(阻止默认行为和阻止事件的捕获或冒泡)
* ========================================================================
* @method stopEvent
* @param {Event} evt - 事件对象
*
* @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('.anchor')
*
* on($nav, 'click', function(evt) {
* console.log('你点击了导航栏')
* })
*
* on($anchor, 'click', function(evt) {
* console.log('tagName', this.tagName)
*
* // 工作台输出:'a'
* // 不会触发事件冒泡,输出:'你点击了导航栏'
* // 也不会切换到 href 属性的页面,阻止了点击链接的默认行为
* stopEvent(evt)
* })
*/
const stopEvent = function (evt) {
stopPropagation(evt)
preventDefault(evt)
}
export default stopEvent