Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

DOM 事件回调

概要

A possible way to get notified of Events of a particular type (such as click) for a given object is to specify the event handler using:

  • An HTML attribute named on{eventtype} on an element, for example: <button onclick="return handleClick(event);">,
  • or by setting the corresponding property from JavaScript, for example: document.getElementById("mybutton").onclick = function(event) { ... }.

If you're working with a modern browser, use addEventListener() instead.

The available events are documented in the events reference.

Details

Definitions

The term event handler may refer to:

  • any function or object registered to be notified of events,
  • or, more specifically, to the mechanism of registering event listeners via on... attributes or properties in HTML and other web APIs, such as <button onclick="alert(this)"> or window.onload = function() { /* ... */ }.

When discussing the various methods of listening to events,

  • event listener refers to a function or object registered via EventTarget.addEventListener(),
  • whereas event handler refers to a function registered via on... attributes or properties.

This page is about the specifics of registering event handlers using on... attributes and properties.

Objects that support event handlers

Event handlers on HTML elements can be set using attributes starting with "on" in the HTML markup (<button onclick="...">) or using Element.setAttribute, and also via the Element's properties named "on..." using JavaScript (i.e. element.onclick = function() { ... }).

Event handlers can also be set using properties on many non-element objects that generate events, including window, document, XMLHttpRequest, and others.

For historical reasons, some attributes/properties on the <body> and <frameset> elements actually set event handlers on their parent Window object. (The HTML specification names these: onblur, onerror, onfocus, onload, onscroll.)

Event handler's parameters, this binding, and the return value

TBD

When the event handler is invoked

TBD (non-capturing listener)

Specifications

Specification Status Comment
WHATWG HTML Living Standard
event handlers
Living Standard  
HTML5
event handlers
Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

Event handler changes in Firefox 9

In order to better match the specifications, and improve cross-browser compatibility, the way event handlers were implemented at a fundamental level changed in Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6).

Specifically, in the past, event handlers were not correctly implemented as standard IDL attributes. In Gecko 9.0, this was changed. Because of this, certain behaviors of event handlers in Gecko have changed. In particular, they now behave in all the ways standard IDL attributes behave. In most cases, this shouldn't affect web or add-on content at all; however, there are a few specific things to watch out for.

Detecting the presence of event handler properties

You can now detect the presence of an event handler property (that is, for example, onload), using the JavaScript in operator. For example:

if ("onsomenewfeature" in window) {
  /* do something amazing */
}

Event handlers and prototypes

You can't set or access the values of any IDL-defined attributes on DOM prototype objects; that means you can't, for example, change Window.prototype.onload anymore. In the past, event handlers (onload, etc) weren't really implemented as IDL attributes in Gecko, so you were able to do this for those. Now you can't. This improves compatibility.

文档标签和贡献者

 此页面的贡献者: ziyunfei, Darrel.Hsu
 最后编辑者: ziyunfei,