JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

17.2.1 Setting Event Handler Properties

You can set an event handler property in JavaScript code, or for document elements, you can set the corresponding attribute directly in HTML. For handler registration by method invocation, there is a standard method, named addEventListener(), that is supported by all browsers except IE8 and before, and a different method, named attachEvent(), for all versions of IE before IE9.

17.2.1 Setting Event Handler Properties

The simplest way to register an event handler is by setting a property of the event target to the desired event handler function. By convention, event handler properties have names that consist of the word “on” followed by the event name: onclick, onchange, onload, onmouseover, and so on. Note that these property names are case sensitive and are written in all lowercase, even when the event type (such as “readystatechange” consists of multiple words. Here are two example event handler registrations:

// Set the onload property of the Window object to a function.

// The function is the event handler: it is invoked when the document loads.

window.onload = function() {

// Look up aelement

var elt = document.getElementById("shipping_address");

// Register an event handler function that will be invoked right

// before the form is submitted.

elt.onsubmit = function() { return validate(this); }


This event handler registration technique works in all browsers for all commonly used event types. In general, all widely implemented web APIs that define events allow handlers to be registered by setting event handler properties.

The shortcoming of event handler properties is that they are designed around the assumption that event targets will have at most one handler for each type of event. If you are writing library code for use in arbitrary documents, it is better to register event handlers using a technique (such as addEventListener()) that will not modify or overwrite any previously registered handlers.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1