自定义事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="div1">div</div> </body> <script> var oDiv = document.getElementById('div1'); bindEvent(oDiv,'click',function(){ alert(1); }); bindEvent(oDiv,'click',function(){ alert(2); }); bindEvent(oDiv,'click',function(){ alert(3); }); bindEvent(oDiv,'show',function(){ alert(4); }); bindEvent(oDiv,'show',function(){ alert(5); }); bindEvent(document,'show',function(){ alert(5); }); fireEvent(oDiv,'show'); function bindEvent(obj,event,fn){ obj.listeners = obj.listeners || {}; obj.listeners[event] = obj.listeners[event] || []; obj.listeners[event].push(fn); obj.addEventListener(event,fn,false); } function fireEvent(obj,event){ var fn = obj.listeners[event]; console.log(fn); for(var i=0;i<fn.length;i++){ obj.listeners[event][i](); } } </script> </html>