//1、添加事件的对象 2、事件名 3、函数 function myAddEvent(obj,sEvent,fn) { if(obj.attachEvent) { obj.attachEvent('on'+sEvent,fn); } else { obj.addEventListener(sEvent,fn,false); } }
例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script> function aaa() { alert('a'); } function bbb() { alert('b'); } //1、添加事件的对象 2、事件名 3、函数 function myAddEvent(obj,sEvent,fn) { if(obj.attachEvent) { obj.attachEvent('on'+sEvent,fn); } else { obj.addEventListener(sEvent,fn,false); } } window.onload=function() { var oBtn=document.getElementById('btn1'); myAddEvent(oBtn,'click',aaa); myAddEvent(oBtn,'click',bbb); } </script> </head> <body> <input type="button" value="点击" id="btn1" /> </body> </html>