<html>
<body>
<p> Internet Explorer 8 及更早IE版本不支持 addEventListener() 方法。</p>
<p>该实例演示了跨浏览器的解决方法。</p>
<p>文档中使用 addEventListener() 方法添加 onmousemove 事件句柄,当鼠标移动时会显示随机数。</p>
<p>点击按钮移除事件句柄。</p>
<button onclick="removeHandler()" id="myBtn">点我</button>
<p id="demo"></p>
<script>
if (document.addEventListener) {
document.addEventListener("mousemove", myFunction);
} else if (document.attachEvent) {
document.attachEvent("onmousemove", myFunction);
}
function myFunction() {
document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler() {
if (document.removeEventListener) {
document.removeEventListener("mousemove", myFunction);
} else if (document.detachEvent) {
document.detachEvent("onmousemove", myFunction);
}
}
</script>
</body>
</html>