Three methods of jquery cancellation of event bubbling (recommended) _jquery

Source: Internet
Author: User

1, by returning false to cancel the default behavior and prevent the event from bubbling.

JQuery Code:

$ ("form"). Bind (

"submit", 

function () {return 

false;

}

);

2. By using the Preventdefault () method, only the default behavior is canceled.

JQuery Code:

$ ("form"). Bind (

"submit", 

function (event) {
event.preventdefault ();
}

);

3. By using the Stoppropagation () method, only one event is prevented from bubbling.

JQuery Code:

$ ("form"). Bind (

"submit", 

function (event) {
event.stoppropagation ();
}

);

(4) Summary

1. an event bubbling corresponds to triggering the same event at the top

Special: If two is set to double-click an event, the event that triggers one click is bubbling when you click Two

(Double-click the Include).

2. If in the Click event, add E.preventdefault () before the event you want to handle;

Then cancel the behavior (Popular understanding: equivalent to doing a return operation), do not execute the following statement.

3. e.stoppropagation () the upper Click event is not triggered as long as in the Click event.

If an event object is provided, this is a non-ie browser if

(e && e.stoppropagation)
//So it supports the Stoppropagation () method
of the Web E.stoppropagation ();
else
//Otherwise, we need to use IE to cancel event bubbling
window.event.cancelBubble = true;
return false;    

The above jquery cancellation event bubbling three methods (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.