获取事件的冒泡路径
/*
* 获取事件冒泡路径,兼容ie11,edge,chrome,firefox,safari
* @param evt
* @returns {*} 由dom元素组成的数组[span,div,...,Window]
*/
function eventPath(evt) {
const path = (evt.composedPath && evt.composedPath()) || evt.path,
target = evt.target;
if (path != null) {
return (path.indexOf(window) < 0) ? path.concat(window) : path;
}
if (target === window) {
return [window];
}
return [target].concat(getParents(target), window);
};
/*
* 获取父节点
* @param node,memo
* @returns {*}
*/
function getParents(node, memo) {
memo = memo || [];
const parentNode = node.parentNode;
if (!parentNode) {
return memo;
} else {
return getParents(parentNode, memo.concat(parentNode));
}
};
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
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
编辑 (opens new window)
上次更新: 2021/03/31, 14:03:00