123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <button id="btn">按钮</button>
- <script>
- var oBtn = document.getElementById("btn");
- // oBtn.onclick = function(){
- // console.log("按钮被点击了")
- // }
- // oBtn.onclick = function(){
- // console.log("hello");
- // }
- // 移除事件
- // oBtn.onclick = null;
- // var a = 10;
- // var a = 20;
- // 通过 addEventListener 绑定事件
- // 接收三个参数 第一个事件名称 第二个事件处理函数 第三个布尔值是否冒泡
- // oBtn.addEventListener("click",function(){
- // console.log("按钮被点击了");
- // });
- // oBtn.addEventListener("click",function(){
- // console.log("hello");
- // });
- // 两个匿名函数 及时内部方法一摸一样 都不是同一个函数
- function foo(){
- console.log("按钮被点击了")
- }
- oBtn.addEventListener("click",foo);
- // 移除事件
- // removeEventListener 要移除的事件内的参数一定要跟绑定事件时的参数一模一样
- oBtn.removeEventListener("click",foo);
- </script>
- </body>
- </html>
|