1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title></title> 6 <style> 7 #div1{width: 200px;height:200px;background-color:red;position: absolute;} 8 </style> 9 <script> 10 function myAddEvent(obj,ev,fn){ 11 if(obj.attachEvent){ 12 obj.attachEvent('on'+ev,fn); 13 } 14 else{ 15 obj.addEventListener(ev,fn,false); 16 } 17 } 18 window.onload=function(){ 19 var oBtn=document.getElementById('btn1'); 20 21 //只弹出 b 22 // oBtn.onclick=function(){ 23 // alert('a'); 24 // } 25 // oBtn.onclick=function(){ 26 // alert('b'); 27 // } 28 29 30 // //IE 31 // //attachEvent(事件名,函数) 32 // //detachEvent(事件名称,函数),解除绑定 33 // oBtn.attachEvent('onclick',function(){ 34 // alert('a'); 35 // }) 36 // oBtn.attachEvent('onclick',function(){ 37 // alert('b'); 38 // }) 39 40 // //FF 41 // //removeEventListener(事件名,函数,捕获) 42 // //addEventListener(事件名,函数,false) 43 // oBtn.addEventListener('click',function(){ 44 // alert('a'); 45 // },false); 46 // oBtn.addEventListener('click',function(){ 47 // alert('b'); 48 // },false); 49 50 // if(oBtn.attachEvent){ 51 // oBtn.attachEvent('onclick',function(){ 52 // alert('a'); 53 // }) 54 // oBtn.attachEvent('onclick',function(){ 55 // alert('b'); 56 // }) 57 // } 58 // else{ 59 // oBtn.addEventListener('click',function(){ 60 // alert('a'); 61 // },false); 62 // oBtn.addEventListener('click',function(){ 63 // alert('b'); 64 // },false); 65 // } 66 67 myAddEvent(oBtn,'click',function(){ 68 alert('a'); 69 }) 70 myAddEvent(oBtn,'click',function(){ 71 alert('b'); 72 }) 73 } 74 </script> 75 </head> 76 <body> 77 <input id="btn1" type="button" value="按钮" /> 78 </body> 79 </html>