1、事件绑定与解除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> window.onload=function () { var ob=document.getElementById(‘b1‘) //IE 7:attachEvent(事件,函数) // ob.attachEvent("onclick",function () {alert(‘a‘)}) // ob.attachEvent("onclick",function () {alert(‘b‘)}) //FF // ob.addEventListener(‘click‘,function () {alert(‘a‘)},false) // ob.addEventListener(‘click‘,function () {alert(‘b‘)},false) //兼容性绑定事件 // if(ob.attachEvent){ // ob.attachEvent("onclick",function () {alert(‘a‘)}) // ob.attachEvent("onclick",function () {alert(‘b‘)}) // }else{ // ob.addEventListener(‘click‘,function () {alert(‘a‘)},false) // ob.addEventListener(‘click‘,function () {alert(‘b‘)},false) // } //事件解除 // ob.detachEvent // ob.removeEventListener myAddEvent(ob,‘click‘,function () {alert(‘a‘)}) myAddEvent(ob,‘click‘,function () {alert(‘b‘)}) } function myAddEvent(obj,event,func){ if (obj.attachEvent) { obj.attachEvent(‘on‘+event,func) } else { obj.addEventListener(event,func,false) } } </script> </head> <body> <input type="button" value="button" id="b1"> </body> </html>
原文地址:http://f1yinsky.blog.51cto.com/12568071/1940248