<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="button" value="按钮" id="btn"> <script> var btn = document.getElementById('btn'); addEventListener(btn, 'click', function () { alert('hello world'); }); addEventListener(btn, 'click', function () { alert('hello itheima'); }); // 处理注册事件的兼容性问题 // eventName, 不带on, click mouseover mouseout function addEventListener(element, eventName, fn) { // 判断当前浏览器是否支持addEventListener 方法 if (element.addEventListener) { element.addEventListener(eventName, fn); // 第三个参数 默认是false } else if (element.attachEvent) { element.attachEvent('on' + eventName, fn); } else { // 相当于 element.onclick = fn; element['on' + eventName] = fn; } } </script> </body> </html>