当前位置:   article > 正文

dom事件冒泡机制_将dom元素置顶首先事件冒泡

将dom元素置顶首先事件冒泡

dom事件冒泡机制

冒泡:html元素触发事件之后会将事件向上冒泡,交给父元素处理,依次向上,直到浏览器窗口window

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #pre {
            width: 200px;
            height: 200px;
            background: #31fdff;
        }
        #child {
            width: 100px;
            height: 100px;
            background: #bfa;
        }
    </style>
</head>
<body>
<div id="pre">
    <div id="child"></div>
</div>
<script>
    document.getElementById('pre').addEventListener('click', function (e) {
      console.log('pre');
    })

    document.getElementById('child').addEventListener('click', function (e) {
      console.log('child');
    })
    window.addEventListener('click', function (e) {
      console.log('window');
    })
</script>
</body>
</html>

  • 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

点击child元素,控制台打印结果

在这里插入图片描述

冒泡路径

Event.composedPath()返回处理事件的元素对象列表,第一个元素为第一个触发事件的元素,最后一个为window对象

示例

document.getElementById('pre').addEventListener('click', function (e) {
    console.log('pre');
    console.log(e.composedPath());
})

document.getElementById('child').addEventListener('click', function (e) {
    console.log('child');
    console.log(e.composedPath());
})
window.addEventListener('click', function (e) {
    console.log('window');
    console.log(e.composedPath());
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

点击child元素,控制台打印结果
在这里插入图片描述

阻断事件向上冒泡

Event.stopPropagation() 阻断事件向父元素冒泡,在当前元素处理之后,父元素不再触发事件

document.getElementById('pre').addEventListener('click', function (e) {
  console.log('pre');
  console.log(e.composedPath());
})

document.getElementById('child').addEventListener('click', function (e) {
  // 阻断事件向上冒泡
  e.stopPropagation()
  console.log('child');
})
window.addEventListener('click', function (e) {
  console.log('window');
  console.log(e.composedPath());
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

点击child元素,控制台打印结果
在这里插入图片描述

阻断事件同级触发

比如同一个元素注册了两个点击事件处理器,如果在第一个注册的事件处理器中阻断了事件的传播,那么第二个注册的事件处理器就不会再触发
没有阻断统计事件触发示例

document.getElementById('child').addEventListener('click', function (e) {
  console.log('child 1');
})
document.getElementById('child').addEventListener('click', function (e) {
  console.log('child 2');
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

点击child元素,控制台打印结果

在这里插入图片描述

阻断统计事件触发示例

document.getElementById('child').addEventListener('click', function (e) {
  e.stopImmediatePropagation()
  console.log('child 1');
})
document.getElementById('child').addEventListener('click', function (e) {
  console.log('child 2');
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

点击child元素,控制台打印结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/862758
推荐阅读
相关标签
  

闽ICP备14008679号