JS Event bubbling

Source: Internet
Author: User

What is the JS event bubbling? :

Fires a class of events on an object (such as clicking the OnClick event), and if this object defines a handler for this event, then this event invokes the handler, and if the event handler is not defined or the event returns true, the event propagates to the object's parent object, from inside to outside, Until it is processed (all the same events of the parent object will be activated), or it reaches the topmost level of the object, the Document object (some browsers are windows).

How do I prevent jquery events from bubbling?

By a small example to explain

such as the above page,
Divided into three layers: Divone is the outer layer, divtwo middle layer, hr_three is the innermost layer;
They all have their own click events, and the innermost a tag has an href attribute.

Run the page, click "Click Me", will pop up: I am the innermost----> I am the middle layer----> I am the outermost
----> then link to Baidu.

This is the event bubbling, originally I only clicked the tag with ID Hr_three, but did perform three alert operations.
Event bubbling process (denoted by tag ID): hr_three----> Divtwo----> Divone. Bubbles from the innermost layers to the outermost layer.

How to stop it?

1.event.stoppropagation ();
<script type= "Text/javascript" >
$ (function () {
$ ("#hr_three"). Click (Function (event) {
Event.stoppropagation ();
Then click on "Click Me" will pop up: I am the innermost layer, then link to Baidu

2.return false;
If the header is joined by the following code
<script type= "Text/javascript" >
$ (function () {
$ ("#hr_three"). Click (Function (event) {
return false;
Then click on "Click Me" will pop up: I am the innermost, but will not execute link to Baidu page

From this we can see:
1.event.stoppropagation ();
Event bubbling is blocked during event processing, but does not halt the default behavior (it performs a hyperlink jump)

2.return false;
Event bubbling is blocked during event processing, and the default behavior is also blocked (for example, a jump that did not perform a hyperlink just now)

There is also a bubbling-related:
3.event.preventdefault ();
If you put it in the Click event of the head a tag, click "Click Me".
Will find it pop up in turn: I am the innermost----> I am the middle tier----> I am the outermost, but finally did not jump to Baidu

Its role is: During event processing, do not halt the event bubbling, but the default behavior (it only executes all the bullets, but did not perform a hyperlink jump)

Related Article

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.