zoukankan      html  css  js  c++  java
  • 阻止事件冒泡

    事件冒泡本身的特性,会带来的坏处,也会带来的好处。

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            .father {
                overflow: hidden;
                 300px;
                height: 300px;
                margin: 100px auto;
                background-color: pink;
                text-align: center;
            }
            
            .son {
                 200px;
                height: 200px;
                margin: 50px;
                background-color: purple;
                line-height: 200px;
                color: #fff;
            }
        </style>
    </head>

    <body>
        <div class="father">
            <div class="son">son儿子</div>
        </div>
        <script>
            // 常见事件对象的属性和方法
            // 阻止冒泡  dom 推荐的标准 stopPropagation() 
            var son = document.querySelector('.son');
            son.addEventListener('click', function(e) {
                alert('son');
                e.stopPropagation(); // stop 停止  Propagation 传播
                e.cancelBubble = true; // 非标准 cancel 取消 bubble 泡泡
            }, false);

            var father = document.querySelector('.father');
            father.addEventListener('click', function() {
                alert('father');
            }, false);
            document.addEventListener('click', function() {
                alert('document');
            })
        </script>
    </body>

    </html>
  • 相关阅读:
    Application和Page详解
    Session解析
    CSS设置技巧
    CSS布局模型
    CSS盒模型
    JAVA -Xms -Xmx -XX:PermSize -XX:MaxPermSize 区别
    设计模式——单例模式
    设计模式——工厂模式
    Go语言学习
    每周一个设计模式
  • 原文地址:https://www.cnblogs.com/yanlei369343/p/13881950.html
Copyright © 2011-2022 走看看