Check Event bubble phase in JavaScript
Description
The following code shows how to check Event bubble phase.
Example
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
span {<!-- w w w. j a v a 2s. c o m-->
background: white;
color: black;
padding: 2px;
cursor: default;
}
</style>
</head>
<body>
<p id="block1">
<span id="mySpan">This is a span. </span>
</p>
<script type="text/javascript">
var mySpan = document.getElementById("mySpan");
var textblock = document.getElementById("block1");
mySpan.addEventListener("mouseover", handleMouseEvent);
mySpan.addEventListener("mouseout", handleMouseEvent);
textblock.addEventListener("mouseover", handleDescendantEvent, true);
textblock.addEventListener("mouseout", handleDescendantEvent, true);
textblock.addEventListener("mouseover", handleBubbleMouseEvent, false);
textblock.addEventListener("mouseout", handleBubbleMouseEvent, false);
function handleBubbleMouseEvent(e) {
if (e.type == "mouseover" && e.eventPhase == Event.BUBBLING_PHASE) {
e.target.style.textTransform = "uppercase";
} else if (e.type == "mouseout" && e.eventPhase == Event.BUBBLING_PHASE) {
e.target.style.textTransform = "none";
}
}
function handleDescendantEvent(e) {
if (e.type == "mouseover" &&
e.eventPhase == Event.CAPTURING_PHASE) {
e.target.style.border = "thn solid red";
e.currentTarget.style.background = "blue";
} else if (e.type == "mouseout"
&& e.eventPhase == Event.CAPTURING_PHASE) {
e.target.style.removeProperty("border");
e.currentTarget.style.removeProperty("border");
}
}
function handleMouseEvent(e) {
if (e.type == "mouseover") {
e.target.style.background = 'black';
e.target.style.color = 'white';
} else {
e.target.style.removeProperty('color');
e.target.style.removeProperty('background');
}
}
</script>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Event Basic
Add click event handler to form and check t...
Create your own event in JavaScript
Demonstrate Event Bubbling in JavaScript
Demonstrate Event Capture and Bubble and ev...
Find out event handling and firing phases i...
Get element id through event object in Java...
Get event Phase in JavaScript
Get event source element name and id in Jav...
Get the event target in JavaScript
Get the event type for key event in JavaScr...
Get the event type from event in JavaScript
Get the source tag name which triggered the...
Prevent event Bubble and Capture in JavaScr...
Remove a certain type of event listener fro...
Show the event capture phase in JavaScript
Stop Event Propagation in JavaScript
Tell if the event supports bubble with Even...
Test the event bubbling in JavaScript
Add click event handler to form and check t...
Check Event bubble phase in JavaScript
Check event type in JavaScriptCreate your own event in JavaScript
Demonstrate Event Bubbling in JavaScript
Demonstrate Event Capture and Bubble and ev...
Find out event handling and firing phases i...
Get element id through event object in Java...
Get event Phase in JavaScript
Get event source element name and id in Jav...
Get the event target in JavaScript
Get the event type for key event in JavaScr...
Get the event type from event in JavaScript
Get the source tag name which triggered the...
Prevent event Bubble and Capture in JavaScr...
Remove a certain type of event listener fro...
Show the event capture phase in JavaScript
Stop Event Propagation in JavaScript
Tell if the event supports bubble with Even...
Test the event bubbling in JavaScript