zoukankan      html  css  js  c++  java
  • JS4 -- 事件流(冒泡和捕获)

    1.事件委托或事件代理

    var oUl = document.getElementById('ul').addEventListener('click',function(){})

    https://www.cnblogs.com/liugang-vip/p/5616484.html

    事件捕获(网景)

    var oDiv = document.getElementById('div').addEventListener("click",function() {},true)

    自外向内(从上级至下级),父级元素向子。。。元素

    事件冒泡(IE -> firefox、chrome、safari、opera)

    冒泡:自内向外(从下至上),子元素向父。。。元素

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
        #father{
            width: 1000px;
            height: 200px;
            background: rgba(255,0,0,.4);
        }
        #son{
            width: 200px;
            height: 200px;
            background: rgba(0,255,0,.4);
        }
        </style> 
    </head>
    <body>
        <div id="father">
            <div id="son">我是儿子</div>
        </div>
        <script>
            // (1)事件冒泡
            // document.getElementById('father').onclick = function() {
            //     alert('father')
            // }
            // document.getElementById('son').onclick = function() {
            //     alert('son')
            // }
         //点击子元素打印 =》 son到father
         // 阻止冒泡: window.event? window.event.cancelBubble = true : e.stopPropagation();
    // (2)事件捕获 document.getElementById('father').addEventListener("click",function(e){ alert('father') },true) document.getElementById('son').addEventListener("click",function(e){ alert('son') },true)
         //点击子元素打印 => father到son
    </script> </body> </html>

     

    JavaScript捕获和冒泡探讨

  • 相关阅读:
    【Java】REST风格
    KMP(烤馍片)算法
    Lca求法 (树链剖分 与 倍增)
    hash学习笔记
    星际网络(数学)
    P3537 [POI2012]SZA-Cloakroom (背包)
    乘车路线 (二维最短路)
    渔民的烦恼 (二分)
    Jmeter 常用函数(18)- 详解 __isDefined
    Jmeter 常用函数(17)- 详解 __substring
  • 原文地址:https://www.cnblogs.com/lgyong/p/9521226.html
Copyright © 2011-2022 走看看