首页
极刻
圈子
教程
开源
极客社区
写文章
发极刻
建圈子
发项目
登录 / 注册
返回教程:Javascript 参考手册
TRY RUN / 在线示例
Javascript 参考手册 尝试一下
Javascript 参考手册 尝试一下
源代码
运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>安科教程(www.ancii.com)</title> <style> #myDIV { background-color: coral; border: 1px solid; padding: 50px; color: white; } </style> </head> <body> <p>Internet Explorer 8 及更早 IE 版本不支持 removeEventListener() 方法。</p> <p>该实例可以在任何浏览器下执行。</p> <div id="myDIV"> div 元素添加了 onmousemove 事件句柄,鼠标在桔红色框内移动过程中会显示随机数。 <p>点击按钮移除 DIV 的事件句柄。</p> <button onclick="removeHandler()" id="myBtn">点我</button> </div> <p id="demo"></p> <script> var x = document.getElementById("myDIV"); if (x.addEventListener) { x.addEventListener("mousemove", myFunction); } else if (x.attachEvent) { x.attachEvent("onmousemove", myFunction); } function myFunction() { document.getElementById("demo").innerHTML = Math.random(); } function removeHandler() { if (x.removeEventListener) { x.removeEventListener("mousemove", myFunction); } else if (x.detachEvent) { x.detachEvent("onmousemove", myFunction); } } </script> </body> </html>
运行结果