Salted fish front end-js event capture / bubbling

Salted fish front end-js event capture / bubbling

Event bubbling

Event bubbling: multiple elements are nested in a hierarchical relationship, and these elements register the same event. After the event of the element inside starts, the event of the element outside also automatically triggers.
E.g:

<!DOCTYPE html>
<html>
<head>
    <title>冒泡车测试</title>
    <style type="text/css">
        #bx1 {
            width: 400px;
            height: 400px;
            background-color: red;
        }

        #bx2 {
            width: 300px;
            height: 300px;
            background-color: blue;
        }

        #bx3 {
            width: 200px;
            height: 200px;
            background-color: yellow;
        }

    </style>
</head>
<div id="bx1">
    <div id="bx2">
        <div id="bx3"></div>
    </div>
</div>
<script>
    var xy$ = function (id) {
        return document.getElementById(id)
    }
    xy$("bx1").onclick = function () {
        console.log(this.id)
    }
    xy$("bx2").onclick = function () {
        console.log(this.id)
    }
    xy$("bx3").onclick = function () {
        console.log(this.id)
    }
</script>
</body>
</html>

Click bx3, both bx1 and bx2 have effect
Insert picture description here

Stop the event from bubbling

Stop event bubbling method

window.event.cancelBubble=true;//谷歌,IE8支持,火狐不支持
e.stopPropagation();//谷歌和火狐支持

and e are window.event event parameter object, is a standard IE, Firefox is a standard
event parameter e in IE8 browser is not present, then instead of using window.event

The above example can become like this: to save space only write JS part

   //阻止事件冒泡函数
    function stopBubble(e) {
        if (e && e.stopPropagation)
            e.stopPropagation()
        else
            window.event.cancelBubble = true
    }

    var xy$ = function (id) {
        return document.getElementById(id)
    };
    xy$("bx1").onclick = function () {
        console.log(this.id)
    };
    xy$("bx2").onclick = function () {
        console.log(this.id)
    };
    xy$("bx3").onclick = function (e) {
        console.log(this.id);
        stopBubble(e)
    }

effect
Insert picture description here

Event phase

1. Event capture phase: from outside to inside
2. Event target phase: the one selected
3. Event bubbling phase: from inside to outside

e.eventPhaseThe current stage of event propagation is returned through this property
* If the value of this property is:
* 1 ----> Capture stage
* 2 ----> Target stage
* 3 ----> Bubbling
*

 	
    var xy$ = function (id) {
        return document.getElementById(id)
    };
    var objs = [xy$("bx3"), xy$("bx2"), xy$("bx1")];
    //遍历注册事件
    objs.forEach(function (ele) {
        //为每个元素绑定事件
        ele.addEventListener("click", function (e) {
            console.log(this.id + ":" + e.eventPhase);
        }, false);
    });


Insert picture description here

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/105030692