编辑下面的代码:【加编码】
<html> <head> <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>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂