教程·Javascript 中文手册
表 9.1列出了本章的一个对象。
表 9.1 与事件相关的对象
| 对象 | 描述 |
|---|---|
| event | Represents a JavaScript event. Passed to every event handler. |
表 9.2总览 JavaScript 事件句柄。
表 9.2 事件及对应的事件句柄。
| 事件 | 事件句柄 | 事件发生的条件…… |
|---|---|---|
| abort | onAbort | The user aborts the loading of an image (for example by clicking a link or clicking the Stop button). |
| blur | onBlur | A form element loses focus or when a window or frame loses focus. |
| change | onChange | A select, text, or textarea field loses focus and its value has been modified. |
| click | onClick | An object on a form is clicked. |
| dblclick | onDblClick | The user double-clicks a form element or a link. |
| dragdrop | onDragDrop | The user drops an object onto the browser window, such as dropping a file on the browser window. |
| error | onError | The loading of a document or image causes an error. |
| focus | onFocus | A window, frame, or frameset receives focus or when a form element receives input focus. |
| keydown | onKeyDown | The user depresses a key. |
| keypress | onKeyPress | The user presses or holds down a key. |
| keyup | onKeyUp | The user releases a key. |
| load | onLoad | The browser finishes loading a window or all of the frames within a FRAMESET
tag. |
| mousedown | onMouseDown | The user depresses a mouse button. |
| mousemove | onMouseMove | The user moves the cursor. |
| mouseout | onMouseOut | The cursor leaves an area (client-side image map) or link from inside that area or link. |
| mouseover | onMouseOver | The cursor moves over an object or area from outside that object or area. |
| mouseup | onMouseUp | The user releases a mouse button. |
| move | onMove | The user or script moves a window or frame. |
| reset | onReset | The user resets a form (clicks a Reset button). |
| resize | onResize | The user or script resizes a window or frame. |
| select | onSelect | The user selects some of the text within a text or textarea field. |
| submit | onSubmit | The user submits a form. |
| unload | onUnload | The user exits a document. |
JavaScript applications in the browser are largely event-driven. Events are actions that occur usually as a result of something the user does. For example, clicking a button is an event, as is changing a text field or moving the mouse over a link. For your script to react to an event, you define event handlers, such as onChangeandonClick.
If an event applies to an HTML tag, then you can define an事件适用对象it. The name of an event handler is the name of the event, preceded by "on". For example, the事件适用对象the focus event is onFocus.
To create an事件适用对象an HTML tag, add an event handler attribute to the tag. Put JavaScript code in quotation marks as the attribute value. The general语法 is
<TAG eventHandler="JavaScript Code"> where TAG is an HTML tag and eventHandler is the name of the event handler. For example, suppose you have created a JavaScript function called compute. You can cause the browser to perform this function when the user clicks a button by assigning the function call to the button's onClick event handler:
<INPUT TYPE="button" VALUE="Calculate" onClick="compute(this.form)"> You can put any JavaScript statements inside the quotation marks following onClick. These statements are executed when the user clicks the button. If you want to include more than one statement, separate statements with a semicolon.
When you create an event handler, the corresponding JavaScript object gets a property with the name of the event handler in lower case letters. (In Navigator 4.0, you can also use the mixed case name of the事件适用对象the property name.) This property allows you to access the object's event handler. For example, in the preceding example, JavaScript creates a
JavaScript Guide contains more information about creating and using event handlers.
In Navigator 4.0, JavaScript includes eventobject is sent as an argument to the event handler.
Typically, the object on which the event occurs handles the event. For example, when the user clicks a button, it is often the button's event handler that handles the event. Sometimes you may want the documentobject to handle all MouseDown events no matter where they occur in the document. JavaScript's event capturing model allows you to define methods that capture and handle events before they reach their intended target.
In addition to providing the Layerobjects have these new methods:
For example, suppose you want to capture all click events that occur in a window. First, you need to set up the window to capture click events:
window.captureEvents(Event.CLICK);
The argument to eventobject and indicates the type of event to capture. To capture multiple events, the argument is a list separated by vertical slashes (|). For example:
window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP)
Next, you need to define a function that handles the event. The argument evnt is the eventobject for the event.
function clickHandler(evnt) {
You have four options for handling the event:Events in Navigator 4.0
//What goes here depends on how you want to handle the event.
//This is described below.
}
Note: When routeEvent calls an event handler, the event handler is activated. If routeEvent calls an event handler whose function is to display a new page, the action takes place without returning to the capturing object.
As long as the link has an onClick handler, the link handles any click event it receives.
Finally, you need to register the function as the window's事件适用对象that event:
window.onClick = clickHandler;
In the following example, the window and document capture and release events:
<HTML>
<SCRIPT>
function fun2(evnt) {
alert ("The document got an event of type: " + evnt.type);
return false;
}
function setWindowCapture() {
window.captureEvents(Event.CLICK);
}
function releaseWindowCapture() {
window.releaseEvents(Event.CLICK);
}
function setDocCapture() {
document.captureEvents(Event.CLICK);
}
function releaseDocCapture() {
document.releaseEvents(Event.CLICK);
window.onclick=fun1;
document.onclick=fun2;