1. Event Flow
The event flow describes the order in which events are received from the page. But IE is proposing a bubbling stream, and Netscape Communicator is proposing a capture stream.
JavaScript Event Stream
2, event bubbling (events bubbling)
The event begins to be received by the most specific element (the node with the deepest nesting level) and then propagated upward to a less specific node (document). As follows:
Copy Code code as follows:
<title> Event Bubbling </title>
<body>
<div id= "Mydiv" > Click me </div>
</body>
Window.onload = function () {
var obj = document.getElementById ("test");
Obj.onclick = function () {
alert (this.tagname);
};
Document.body.onclick = function () {
alert (this.tagname);
};
Document.documentElement.onclick = function () {
alert (this.tagname);
};
Document.onclick = function () {
Alert ("Document");
};
Window.onclick = function () {
Alert ("window");
}
};
Event Propagation Order: Div-->body-->html-->document
Attention:
All modern browsers support bubbling events, but there are some differences in implementation. Event bubbling from the IE5.5 and earlier versions jumps directly from the body to document (does not execute HTML). Firefox, Chrome, and Safari keep the event bubbling to the Window object.
3. Stop event bubbling and cancel default events
A. Getting event objects
Copy Code code as follows:
function GetEvent (event) {
Window.event IE
Event Non IE
Return Event | | window.event;
}
b Function: Stop event bubbling
Copy Code code as follows:
function Stopbubble (e) {
If an event object is provided, this is a non-IE browser
if (e && e.stoppropagation) {
So it supports the Stoppropagation () method of the Consortium
E.stoppropagation ();
} else {
Otherwise, we need to use IE to cancel event bubbling
Window.event.cancelBubble = true;
}
}
C. Blocking default behavior for browsers
Copy Code code as follows:
function Stopdefault (e) {
Block default browser actions (the Web-consortium)
if (e && e.preventdefault) {
E.preventdefault ();
} else {
How to block the default action of a function in IE
Window.event.returnValue = false;
}
return false;
}