<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>事件函数</title> </head> <body> <div id="div1">div1</div> <input type="text" name="a" id="input1"> <a href="http://www.baidu.com" target="_blank" id="a1">baidu</a> <script src="dom2.js"></script> </body> </html>
var div1 = document.getElementById("div1"); div1.addEventListener("click",add); var input1=document.getElementById("input1"); input1.addEventListener("keydown",add); function add(event){ var e = event || window.event;//ie8 console.log(event); // 取消默认操作 // event.preventDefault(); // 用return false 也可以达到 上面的效果 // 但是 必须用 a1.onclick = add; return false // ie8 event.returnValue = false; } var a1= document.getElementById("a1"); a1.addEventListener("click",add); // a1.onclick = add;