DOM是事件监听器
DOM事件监听器予许一个事件触发多个方法,在实际工作中应用比较多。
<!DOCTYPE html>
<html> <head> <title>DOM事件监听器1</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <button id='a'>按钮</button> <script> var elbtn = document.getElementById('a'); elbtn.addEventListener('click', msg, false); function msg(){ alert("DOM事件监听器"); } </script> </body></html>在监听器中调用到参数函数
<!DOCTYPE html>
<html> <head> <title>DOM事件监听器2</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <button id="a">按钮</button> <script> var elbtn = document.getElementById('a'); elbtn.addEventListener('click', function () { say('DOM'); }, false); function say(name) { alert('你好,' + name + "!"); } </script> </body></html>