DOM—addEventListener() & removeEventListener()

DOM EventListener:
方法:
addEventListener():用于向指定元素添加事件句柄
removeEventListener():移除方法添加的事件句柄

<body>
    <button id="btn">按钮</button>
    <script>
        //放在button标签后面,放在前面,无法获取标签
        var x = document.getElementById("btn");
        x.addEventListener("click", hello);//添加句柄
        x.addEventListener("click", world);//为事件添加多个句柄,不会被覆盖
        x.removeEventListener("click", hello);//移除句柄
        function hello() {
            alert("Hello");
        }
        function world() {
            alert("World");
        }
    </script>
</body>
原文地址:https://www.cnblogs.com/feile/p/5449593.html