• 删除事件


     

     案例

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            div {
                 100px;
                height: 100px;
                background-color: pink;
            }
        </style>
    </head>

    <body>
        <div>1</div>
        <div>2</div>
        <div id="box">3</div>
    </body>
    <script>
        var divs = document.querySelectorAll('div');
        var box = document.getElementById('box');

        // 1
        // divs[0].onclick = function() {
        //         alert(11);
        //         // 传统方式删除事件
        //         divs[0].onclick = null;
        //     }
        //     // 2
        // divs[1].addEventListener('click', fn)

        // function fn() {
        //     alert(22);
        //     // 移除事件
        //     divs[1].removeEventListener('click', fn);
        // };

        // 3 
        box.attachEvent('onclick', fn1);

        function fn1() {
            alert(33);
            box.detachEvent('onclick', fn1);
        }
    </script>

    </html>
  • 相关阅读:
    深入理解C++右值引用
    并发编程的原子性和顺序性
    LLVM简介
    APK及相关的Android路径
    UE4资源移动与跨项目迁移
    OpenGL简介
    IDEA无限试用插件
    使用idea搭建springcloud
    .NET Core 微服务架构 Steeltoe 使用(基于 Spring Cloud)
    微服务:注册中心ZooKeeper、Eureka、Consul 、Nacos对比
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13054429.html
Copyright © 2020-2023  润新知