亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

js事件冒泡、事件捕獲、蒙版彈框運用

首先简单解释下事件冒泡与事件捕获

事件冒泡:当事件在某一DOM元素被触发时,事件将跟随着该节点继承自的各个父节点冒泡穿过整个的DOM节点层次,如果不停止事件的传播,事件将一直通过DOM冒泡直至到达文档根。

<div class="test1">
    <p>第一层</p>
    <div class="test2">
        <p>第二层</p>
        <div class="test3">
            <p>第三层</p>
        </div>
    </div>
</div>
</body>
<script>
document.querySelector(".test1").onclick = function(){
    console.log("这是第一层");
};
document.querySelector(".test2").onclick = function(){
    console.log("这是第二层");
};
document.querySelector(".test3").onclick = function(event){
        console.log("这是第三层");
};
</script>

https://img1.sycdn.imooc.com//5c1afcf40001918c19050256.jpg

https://img1.sycdn.imooc.com//5c1afd6e0001e9ca11460324.jpg

https://img1.sycdn.imooc.com//5c1afdd900015f3b10590372.jpg

从上面简单的例子中可以得出,冒泡事件就是在执行自身事件后,依次执行父辈事件的一种现象。

如果想阻止这种现象,可以通过添加event.stopPropagation()方法阻止冒泡事件。

https://img1.sycdn.imooc.com//5c1b00fb0001f8db11840577.jpg

//兼容性写法
    if(event && event.stopPropagation){
        event.stopPropagation()
    }else{     //兼容ie
        event.cancelBubble = true
    };

注:jq中同样可以通过stopPropagation方法阻止冒泡事情

事件捕获:事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件。(与冒泡相反)

https://img1.sycdn.imooc.com//5c1ca72000013a7812940724.jpg

https://img1.sycdn.imooc.com//5c1ca7590001b2fc10660396.jpg

实用例子:蒙版弹框

https://img1.sycdn.imooc.com//5c1cb3c800015b3010900729.jpg

https://img1.sycdn.imooc.com//5c1cb4840001c4ad12350700.jpg

    <style>
      *{  margin: 0;  padding: 0;  }
      .testBody{  background-image: url("images/海洋天空.jpg");  background-size: cover;  height: 120vh;  }
      .testMask{  
          display: none;  justify-content:center;  align-items:center;  position: absolute;  top: 0;  left: 0;  width: 100vw;  height: 100vh;  background-color: rgba(0,0,0,.3);  z-index: 100;  }
      .testContent{  width: 50%;  height: 20%;   padding: 20px;  background-color: white}
      .testBody2{ height: 100vh; overflow: hidden }
    </style>
</head>
<body>
<div class="testBody">
    <button class="openMack">打开蒙版</button>
</div>
<div class="testMask">
    <div class="testContent">
        <h3>这里是测试内容</h3>
        <form>
            <input>
            <button>测试提交</button>
        </form>
    </div>
</div>
</body>
<script>
    var testBody = document.querySelector(".testBody");
    var testMack = document.querySelector(".testMask");
    var testContent = document.querySelector(".testContent");
    document.querySelector(".openMack").onclick = function(){
        testBody.setAttribute("class","testBody testBody2");
        testMack.style.display = "flex";
    }
    testMack.onclick = function(){
        testBody.setAttribute("class","testBody");
        testMack.style.display = "none";
    }
    testContent.onclick = function(event){
        event.stopPropagation();
    }
</script>


點擊查看更多內容
2人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
76
獲贊與收藏
535

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消