DOM:event
From MDC
目录 |
[编辑] 介绍
本章讲述 Gecko 所实现的 DOM Level 2 事件模型。 主要内容有Event 接口,以及用于注册 DOM 中结点事件的接口、事件处理函数和事件监听器,还有几个较长的示例用于演示各种事件接口如何彼此关联。
在 DOM Level 3 Events draft 中有一个很好的示意图,可以清楚地解释事件在 ROM 中传递的三个阶段。
[编辑] DOM Event 接口
Event handlers may be attached to various elements in the DOM. When an event occurs, an event object is dynamically created, and passed sequentially to the event listeners that are allowed to handle the event. The DOM Event interface is then accessible within the handler function, via the event object passed as the first (and the only) argument.
The following simple example shows how an event object is passed to the event handler function, and can be used from within one such function.
Note there is no "evt" parameter passed in the code below. The event object gets passed automatically to foo. All that is needed is to define a parameter in the event handler to receive the event object.
function foo(evt) {
// event handling functions like this one
// get an implicit reference to the event object they handle
// (in this case we chose to call it "evt").
alert(evt);
}
table_el.onclick = foo;
This example is woefully simplistic, but it shows an important feature of events in the Gecko DOM, which is that event objects in the DOM are typically accessed in the event handler functions. Once you have a reference to the event object, you can access all of the properties and methods described in this chapter.
Also see Example 5: Event Propagation in the Examples chapter for a more detailed example of how events move through the DOM.
[编辑] DOM event handler List
In addition to the event object described here, the Gecko DOM also provides methods for registering event listeners on nodes in the DOM, removing those event listeners, and dispatching events from the DOM. These and the various Event Handlers on HTML or XML elements are the main entry points for events in the DOM. These three methods are described in the DOM Element Reference list.
You can also pass the event object reference as a predefined parameter, named event, to the function that handles the event. This is very similar to the way this works, but for event objects, rather than element object references.
<html>
<head>
<title>event object parameter example</title>
<script type="text/javascript">
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}
</script>
</head>
<body onmousedown="showCoords(event)">
<p>To display the mouse coordinates click anywhere on the page.</p>
</body>
</html>
Using the predefined event object parameter allows you to pass other parameters to the event handling function as well, if required:
<html>
<head>
<title>event object & extra parameters example</title>
<script type="text/javascript">
var par2 = 'hello';
var par3 = 'world!';
function showCoords(evt, p2, p3){
alert(
"clientX value: " + evt.clientX + "\n"
+ "clientY value: " + evt.clientY + "\n"
+ "p2: " + p2 + "\n"
+ "p3: " + p3 + "\n"
);
}
</script>
</head>
<body onmousedown="showCoords(event, par2, par3)">
<p>To display the mouse coordinates please click anywhere on the page.</p>
</body>
</html>
[编辑] 属性
- event.altKey
- Returns a boolean indicating whether the
<alt>key was pressed during the event. - event.bubbles
- Returns a boolean indicating whether the event bubbles up through the DOM or not.
- event.button
- Returns a mouse key.
- event.cancelBubble
- Deprecated Returns a boolean indicating whether the bubbling up of the event has been canceled or not.
- event.cancelable
- Returns a boolean indicating whether the event is cancelable.
- event.charCode
- Returns the Unicode value of a character key that was pressed as part of a keypress event.
- event.clientX
- Returns the horizontal position of the event.
- event.clientY
- Returns the vertical position of the event.
- event.ctrlKey
- Returns a boolean indicating whether the
<ctrl>key was pressed during the event. - event.currentTarget
- Returns a reference to the currently registered target for the event.
- event.detail
- Returns detail about the event, depending on the type of event.
- event.eventPhase
- Used to indicate which phase of the event flow is currently being evaluated.
- event.explicitOriginalTarget
- The explicit original target of the event (Mozilla-specific).
- event.isChar
- Returns a boolean indicating whether the event produced a key character or not.
- event.keyCode
- Returns the Unicode value of a non-character key in a keypress event or any key in any other type of keyboard event.
- event.layerX
- Returns the horizontal coordinate of the event relative to the current layer.
- event.layerY
- Returns the vertical coordinate of the event relative to the current layer.
- event.metaKey
- Returns a boolean indicating whether the
metakey was pressed during the event. - event.originalTarget
- The original target of the event, before any retargetings (Mozilla-specific).
- event.pageX
- Returns the horizontal coordinate of the event relative to the page.
- event.pageY
- Returns the vertical coorindate of the event relative to the page.
- event.relatedTarget
- Identifies a secondary target for the event.
- event.screenX
- Returns the horizontal position of the event on the screen.
- event.screenY
- Returns the vertical position of the event on the screen.
- event.shiftKey
- Returns a boolean indicating whether the
<shift>key was pressed when the event was fired. - event.target
- Returns a reference to the target to which the event was originally dispatched.
- event.timeStamp
- Returns the time that the event was created.
- event.type
- Returns the name of the event (case-insensitive).
- event.view
- The view attribute identifies the
AbstractViewfrom which the event was generated. - event.which
- Returns the Unicode value of a key in a keyboard event, regardless of which type of key is pressed.
[编辑] 方法
- event.initEvent
- Initializes the value of an Event created through the
DocumentEventinterface. - event.initKeyEvent
- Initializes a keyboard event. Gecko-specific.
- event.initMouseEvent
- Initializes a mouse event once it's been created
- event.initUIEvent
- Initializes a UI event once it's been created.
- event.preventBubble
- Template:obsolete inline Prevents the event from bubbling. This method is deprecated in favor of standard stopPropagation and is removed in Gecko 1.9.
- event.preventCapture
- Template:obsolete inline This method is deprecated in favor of standard stopPropagation and is removed in Gecko 1.9.
- event.preventDefault
- Cancels the event (if it is cancelable).
- event.stopPropagation
- Stops the propagation of events further along in the DOM.