使用方法
target.addEventListener(type, listener, useCapture);
target 文档节点、document、window 或 XMLHttpRequest。
type 字符串,事件名称,不含“on”,比如“click”、“mouseover”、“keydown”等。
listener 实现了 EventListener 接口或者是 JavaScript 中的函数。
useCapture 是否使用捕捉,看了后面的事件流一节后就明白了,一般用 false
事件触发时,会将一个 Event 对象传递给事件处理程序,比如:
document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false);
实例
document.body.addEventListener('mousemove', function moveHandler(e){ if ( e.clientY > 1200 ) doSomething(); // 如果 y > 1200 做你要做的事 document.body.removeEventListener('mousemove', moveHandler, false); }, false);
DOM1 协定
onblur()
The element has lost focus (that is, it is not selected by the user).
onchange0
The element has either changed (such as by typing into a text field) or the element has lost focus.
onclick0
The mouse has been clicked on an element.
ondblclick()
The mouse has been double-clicked on an element.
onfocus()
The element has gotten focus.
onkeydown()
A keyboard key has been pressed down (as opposed to released) while the element has focus.
onkeypress()
A keyboard key has been pressed while the element has focus.
onkeyup()
A keyboard key has been released while the element has focus.
onload()
The element has loaded (document, frameset, or image).
onmousedown()
A mouse button has been pressed.
onmousemove()
The mouse has been moved.
onmouseout()
The mouse has been moved off of or away from an element.
onmouseover()
The mouse has moved over an element.
onmouseup()
A mouse button has been released.
onreset()
The form element has been reset, such as when a form reset button is pressed.
onresize()
The window's size has been changed.
onselect()
The text of a form element has been selected.
onsubmit()
The form has been submitted.
onunload()
The document or frameset has been unloaded.
DOM2 的进化
onblur()
blur
onfocus()
focus
onchange()
change
onmouseover()
mouseover
onmouseout()
mouseout
onmousemove()
mousemove
onmousedown()
mousedown
onmouseup()
mouseup
onclick()
click
ondblclick()
dblclick
onkeydown()
keydown
onkeyup()
keyup
onkeypress()
keypress
onsubmit()
submit
onload()
load
onunload()
unload