javascript-事件綁定
阿新 • • 發佈:2017-06-20
javascript-事件綁定
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>
javascript-事件綁定