Java Reference
In-Depth Information
{
alert(“You moved over the Special Text”);
}
}
document.getElementById(“p”).ondblclick = handle;
document.getElementById(“h1”).onclick = handle;
document.getElementById(“span”).onmouseover = handle;
</script>
This code is slightly different from the last example using HTML attributes. The elements are given
id
attributes to allow easy access to their objects in the DOM with the
getElementById()
method. Each
element is assigned an event handler, which calls
handle()
when their respective events fi re. The result
is the same as before; the user sees an alert box telling them they moved their mouse pointer over the
Special Text.
Using Event Data
The standard outlines several properties of the
Event
object that offer information about that event: what
element it happened at, what type of event took place, and what time it occurred? These are all pieces of
data offered by the
Event
object. The following table lists the properties outlined in the specifi cation.
Properties of the Event Object
Description
bubbles
Indicates whether an event can
bubble
—
passing control from
one element to another starting from the event target and bub-
bling up the hierarchy.
cancelable
Indicates whether an event can have its default action canceled.
currentTarget
Indicates which the event target whose event handlers are cur-
rently being processed.
eventPhase
Indicates which phase of the event fl ow an event is in.
target
Indicates which element caused the event; in the DOM event
model, text nodes are a possible target of an event.
timestamp
Indicates at what time the event occurred.
type
Indicates the name of the event.
Secondly, the DOM event model introduces a MouseEvent object, which deals with events generated
specifi cally by the mouse. This is useful because you might need more specifi c information about the
event, such as the position in pixels of the cursor, or the element the mouse has come from.
Properties of the
MouseEvent Object
Description
altKey
Indicates whether the Alt key was pressed when the event was generated.
button
Indicates which button on the mouse was pressed.
Continued