zoukankan      html  css  js  c++  java
  • JS事件流模型

    JS事件流模型

    (一)JS中的事件流模型

    1. 事件冒泡(fasle/不写):当触发一个节点的事件是,会从当前节点开始,依次触发其祖先节点的同类型事件,直到DOM根节点。
    2. 事件捕获(true):当初发一个节点的事件时,会从DOM根节点开始,依次触发其祖先节点的同类型事件,直到当前节点自身。
    3. 什么时候事件冒泡?什么时候事件捕获?
     ① 当使用addEventListener绑定事件,第三个参数传为true时表示事件捕获;
     ② 除此之外的所有事件绑定均为事件冒泡。

    4. 阻止事件冒泡:

     ① IE10之前,e.cancelBubble = true;
     ② IE10之后,e.stopPropagation();

    5. 阻止默认事件:

     ① IE10之前:e.returnValue = false;
     ② IE10之后:e.preventDefault();

    //css
    #div1{
       300px;;
      height: 300px;
      
    }
    #div2{
       200px;
      height: 200px;
      
    }
    #div3{
       100px;
      height: 100px;
       padding: 0px 0px 0px 5px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 3px solid rgb(108, 226, 108); line-height: 20px; 640px; clear: both; outline: 0px !important; border-radius: 0px !important; border-top: 0px !important; border-right: 0px !important; border-bottom: 0px !important; border-image: initial !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important;">#A9A9A9;
    }
     
    //html
    <div id="div1">
      <div id="div2">
        <div id="div3"></div>
      </div>
    </div>
    <a href="01-事件笔记.html" rel="external nofollow" onclick="func()">超链接</a>
    div1.addEventListener("click",function(){
      console.log("div1 click");
    },false);
    div2.addEventListener("click",function(){
      console.log("div2 click");
    },false);
    div3.addEventListener("click",function(){ //原来的顺序是:3-->2-->1。
    //  myParagraphEventHandler(); //截获事件流后,只触发3.但是从2开始依然会冒泡;
      console.log("div3 click");
    },false);

    结果(事件冒泡)(由小到大div3-》div2-》div1):

    div1.addEventListener("click",function(){
      console.log("div1 click");
    },true);
    div2.addEventListener("click",function(){
      console.log("div2 click");
    },true);
    div3.addEventListener("click",function(){
    //  myParagraphEventHandler(); //截获事件流后,只触发3.但是从2开始依然会冒泡;
      console.log("div3 click");
    },true);

    结果(事件捕获)(由小到大div3-》div2-》div1):

    //依然遵循事件冒泡
    document.onclick=function(){
      console.log("document click")
    }
    //截获事件流阻止事件冒泡
    function myParagraphEventHandler(e) {
       e = e || window.event;
      if (e.stopPropagation) {
         e.stopPropagation(); //IE10以后
      else {
         e.cancelBubble = true//IE10之前
       }
    }
    //截获事件流阻止事件冒泡
    function myParagraphEventHandler(e) {
       e = e || window.event;
      if (e.stopPropagation) {
         e.stopPropagation(); //IE10以后
      else {
         e.cancelBubble = true//IE10之前
       }
    }
    //阻止默认事件
    function eventHandler(e) {
      e = e || window.event;
    // 防止默认行为
       if (e.preventDefault) {
         e.preventDefault(); //IE10之后
      else {
         e.returnValue = false//IE10之前 
      }
    }

  • 相关阅读:
    搜索回车跳转页面
    登录验证码
    【排序算法】排序算法之插入排序
    PAT 乙级 1044 火星数字 (20 分)
    PAT 甲级 1035 Password (20 分)
    PAT 甲级 1041 Be Unique (20 分)
    PAT 甲级 1054 The Dominant Color (20 分)
    PAT 甲级 1027 Colors in Mars (20 分)
    PAT 甲级 1083 List Grades (25 分)
    PAT 甲级 1005 Spell It Right (20 分)
  • 原文地址:https://www.cnblogs.com/login123/p/12130427.html
Copyright © 2011-2022 走看看