• 冒泡的问题及阻止冒泡


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Document</title>
    </head>
    <body>
    <button id="btn">点击</button>
    </body>
    </html>
    <script>
        var btn=document.getElementById("btn");  //冒泡的问题,当点击按钮时,先是弹出“点击了按钮”,后又弹出"点击了空白处",这是因为,按钮也是文档中的部分;
                                                 // 阻止冒泡就是点击按钮时,只弹出”点击了按钮“;
        document.onclick=function () {
            alert("点击了空白处");
        }
        btn.onclick=function (event) {
            alert("点击了按钮");
            var event=event||window.event;
            if(event && event.stopPropagation)
            {
                event.stopPropagation(); //w3c标准
            }
            else{
                event.cancelBubble=true;//ie678
            }
        }
    
    </script>
    

      

  • 相关阅读:
    scnner02 (nextLine)
    Scanner01
    Spring 框架 (初学)
    查询自己写了多少行代码
    jdbc事务
    jdbc(预编译插入数据)
    jdbc(java连接数据库)
    监听器扩展
    listener(监听器)
    Filter过滤器
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/11252877.html
Copyright © 2020-2023  润新知