On the event _javascript skills of JavaScript

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.