Review DHTML(msdn):About the DHTML Object Model (2)

Understanding the Event Model

The Life Cycle of an Event

An event has a life cycle that begins with the action or condition that initiates the event and ends with the final response by the event handler or Microsoft Internet Explorer. The life cycle of a typical event consists of the following steps.

  1. The user action or condition associated with the event occurs.
  2.  The event object is instantly updated to reflect the conditions of the event.
  3. The event fires. This is the actual notification in response to the event.
  4. The event handler associated with the source element is called, carries out its actions, and returns.
  5. The event bubbles up to the next element in the hierarchy, and the event handler for that element is called. This step repeats until the event bubbles up to the window object or a handler cancels bubbling.
  6. The final default action, if any, is taken, but only if this action has not been canceled by a handler.

Attaching an Event Handler

Declare an event handler function and assign a call to that function in the appropriate inline event attribute of an HTML tag.
Declare event handling code and use the FOR and
event attributes of the script tag to associate the code with the event.
<SCRIPT FOR=MyImage EVENT=onmouseover LANGUAGE="JScript">
    // Carry out some work
</SCRIPT>
 
<IMG ID=MyImage SRC="sample.gif">

JScript event handler

    When set using an inline event attribute, associates when the element is created.

FOR and EVENT event handling code

    Associates when the document is fully loaded.

Attaching with Event Properties

<H1 id=MyHeading>Welcome!</H1>

   
<SCRIPT>

function setHeadStyle() {

    window.event.srcElement.style.color = "green";

}

document.all.MyHeading.onclick=setHeadStyle;

</SCRIPT>

Returning Values and Canceling Default Actions

Event handlers can return values to the event either by using the return value mechanism
defined for the language, such as the return statement, or by using the
returnValue property of the event object.

Return values are useful for controlling the default actions associated with events. For
example, clicking the
a element causes the browser to load the document specified by the
HREF attribute.
You can cancel default actions such as these by returning false from your event handlers.

Not all scripting languages support return values. For these languages, you must use the
returnValue property to set a return value. If a language supports return values, you can use either the language-defined method of returning a value or returnValue. If you use both methods, the property always takes precedence.
 

Event Object

The event object, on the window object, is accessible to all event handlers and is the
language-independent, preferred way for handlers to get and modify information about the event.(window.event.srcElement.tagName)

Keyboard Events


onkeydown
   
onkeyup  onkeypress
When a keyboard event occurs, the keyCode property of the event object contains the
Unicode keycode of the corresponding key. The altKey, ctrlKey, and shiftKey properties
specify the state of the ALT, CTRL, and SHIFT keys.You can change which key is associated
with the event by either changing the value of the keyCode property or returning an
integer value. You can cancel the event by returning zero or false.

The onhelp event is a special keyboard event that occurs when the user presses the help key (F1).


For a complete list, see
DHTML Events.

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/om/event_model.asp

posted on 2004-08-09 14:10  9yue  阅读(560)  评论(0)    收藏  举报