Java Reference
In-Depth Information
event.target.classList.toggle("highlight");
}
Now if you double click on the second paragraph, it should change color as the class of
highlight
is toggled on and off.
The
mouseover
event occurs when the mouse pointer is placed over the element to which
the event listener is attached, while the
mouseout
event occurs when the mouse pointer
moves away from an element. This example uses both the
mouseover
and
mouseout
events to change the color of the third paragraph (with an ID of "
mouse
") when the mouse
pointer hovers over it, and back again when it moves away from the paragraph:
var mouse = document.getElementById("mouse");
mouse.addEventListener("mouseover", highlight);
mouse.addEventListener("mouseout", highlight);
The
mousemove
event occurs whenever the mouse moves. It will only occur while the
cursor is over the element to which it is applied. The following line of code creates an alert
dialog whenever the mouse moves over the third paragraph:
mouse.addEventListener("mousemove", function() {
console.log(
↵
"You Moved!"); } );
Keyboard Events
There are three events that occur when keys are pressed:
keydown
,
keypress
, and
keyup
. When a user presses a key, the events occur in that order. They are not tied to any
particular key, although the information about which key was pressed is a property of the
event object.
1. The
keydown
event occurs when a key is pressed and will
continue to occur
if
the key is held down.
2. The
keypress
event occurs after a
keydown
event but before a
keyup
event.
The
keypress
event only occurs for keys that produce character input. This
