Home > Web Front-end > JS Tutorial > Usage example of javascript bubbling event (code)

Usage example of javascript bubbling event (code)

不言
Release: 2018-10-27 15:20:34
forward
1487 people have browsed it

The content of this article is about the usage examples (code) of JavaScript bubbling events. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Note: When the lower element and the upper element support the same event, when the upper event is triggered, the lower event is also triggered. This is called event bubbling.

Cancel event bubbling: ev.cancelBubble = true

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
        body{
        background:yellow;}
        div{
        width:300px;
        height:300px;
        background:green;
        }
        </style>
    </head>
    <body  onclick="di()">
        <div onclick="ding()"></div>    
    </body >
    <script>
    function di(){
    alert(&#39;底层&#39;)
    
    }
    function ding(e){
    var ev = e|| event

// 取消事件冒泡,其实是阻止事件向下传递
            ev.cancelBubble = true
    alert(&#39;上层社会&#39;)
    }
    
    </script>

</html>
Copy after login

Clicking the upper div will also trigger the lower event. To cancel bubbling, just: ev.cancelBubble = true

Usage example of javascript bubbling event (code)

The above is the detailed content of Usage example of javascript bubbling event (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template