zoukankan      html  css  js  c++  java
  • 事件流

    1 <div class="outer" id="outer" >
    2         <div class="inner" id="inner">
    3             
    4         </div>
    5     </div>
    6     <input type="button" value="测试解绑" id="unbind" />
    7     <p id="text">劲爆鸡米花</p>
     1 <script type="text/javascript">
     2 
     3     // ================= 捕获与冒泡
     4     var outer = document.getElementById("outer"),
     5         inner = document.getElementById("inner");
     6 
     7     fn1 = function(e){
     8         console.log(e.target.id);    
     9         //console.log(this.id);        // 始终是 outer  e.target 不是任何时候都是指向 this 的。 this 始终指向的是绑定事件的元素
    10         console.log("1");
    11     }
    12     
    13     outer.addEventListener("click",fn1, false);// 第三个参数设置为true -事件发生在捕获阶段; false- 冒泡阶段 inner  / outer
    14 
    15     // 点击inner 的时候,先发生捕获,在输出2; But 将outer 绑定的 事件 设置为冒泡阶段,输出顺序将会颠倒
    16     inner.addEventListener("click", function(e){
    17         console.log(2);
    18         console.log(e.cancelBubble);
    19         if(e.stopPropagation){
    20             console.log(3)
    21             e.stopPropagation();
    22         }
    23         e.cancelBubble = true;
    24     }, false);
    25 
    26     // =============== 解除绑定
    27     var $unbind = document.getElementById("unbind");
    28 
    29     fn3 = function(){
    30         console.log("解绑", this.id);
    31     }
    32     /*$unbind.addEventListener("click" , function(){
    33         console.log("解绑", this.id);
    34     }, false);
    35 
    36     $unbind.removeEventListener("click", function(){    // 不起作用
    37         console.log("解绑", this.id);
    38     }, false);*/
    39 
    40     $unbind.addEventListener("click" , fn3, false);
    41 
    42     $unbind.removeEventListener("click", fn3, false); // 起作用 因为上面的其实 不是解绑的同一个function 并且 参数应当设置为一致
    43 
    44 
    45     //如果你的文档结构非常复杂,你可以通过阻止冒泡来节约系统资源。毕竟,浏览器必须查询事件目标的每一个祖先元素是否有绑定相同的事件,即使一个也没有找到,这个查询的过程仍然需要花费时间
    46 
    47 
    48     // IE attachEvent
    49     var btn = document.getElementById("text");
    50     /*btn.attachEvent("onclick", function(e){    // chrome 就会报错啦
    51         console.log(this === window);        // true
    52         console.log(this)    // window      attachEvent 中 this  指向 window; 在使用DOM0 级方法的情况下,事件处理程序会运行在所属元素的作用域内
    53 
    54         var target = e.target || e.srcElement;
    55         console.log(target.id)    // text
    56     });
    57     btn.attachEvent("onclick", function(){
    58         console.log("i come firstly?!");    // IE8,7 下面会先输出这个
    59     });*/
    60 
    61 
    62 
    63     // 兼容浏览器
    64     var addEvent = function(el, type, fn){
    65         el.addEventListener ? el.addEventListener(type, fn, false) : el.attachEvent("on" + type, function(){
    66             fn.apply(el, arguments);
    67         });
    68     };
    69     addEvent(btn, "click", function(){console.log(this)});
    70     
    71 
    72 
    73     </script>
    疯癫不成狂,有酒勿可尝;世间良辰美,终成水墨白。
  • 相关阅读:
    Unix命令大全
    vs2008 与 IE8出现的兼容性问题
    Java 创建文件、文件夹以及临时文件
    如何修改Wamp中mysql默认空密码
    PAT 乙级真题 1003.数素数
    Tags support in htmlText flash as3
    DelphiXE4 FireMonkey 试玩记录,开发IOS应用 还是移植
    10 Great iphone App Review sites to Promote your Apps!
    HTML tags in textfield
    Delphi XE4 IOS 开发, "No eligible applications were found“
  • 原文地址:https://www.cnblogs.com/chuyu/p/3342168.html
Copyright © 2011-2022 走看看