<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <div id="wrap"> <input type="button" id="btn" value="attachEvent" /> </div> <script type="text/javascript"> window.onload = function() { function iniEvent2() { var btn = document.getElementById('btn'); btn.attachEvent('onclick', click1); btn.attachEvent('onclick', click2); btn.attachEvent('onclick', click3); } function click1() { alert('1'); } function click2() { alert('2'); } function click3() { alert('3'); } iniEvent2(); } </script> </body> </html>
在IE下使用,一个按钮可以有很多个方法,下面的是除了IE下的使用,false是冒泡阶段
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <div id="wrap"> <input type="button" id="btn" value="attachEvent" /> </div> <script type="text/javascript"> window.onload = function() { function iniEvent2() { var btn = document.getElementById('btn'); btn.addEventListener('click', click1, false); btn.addEventListener('click', click2, false); btn.addEventListener('click', click3, false); } function click1() { alert('1'); } function click2() { alert('2'); } function click3() { alert('3'); } iniEvent2(); } </script> </body> </html>