jquery Blocks Event bubbling event.stoppropagation_jquery

Source: Internet
Author: User
Description: Prevents events from bubbling onto the DOM tree, which is the event handler on any predecessor element that is not triggered.

Version added:1.0
Event.stoppropagation ()
We can use event.ispropagationstopped () to determine whether this method has been invoked (on that event object).

This method is also valid for trigger ()-defined events.

Note that this does not prevent other event handler functions on the same element from running.

Additional Notes:
Live events are unlikely to stop propagating once the live event has been propagated to the top of the document. Similarly, the. Delegate () event will always propagate to the contained delegate element, and the events on the element will be executed when the delegate event is invoked.
Example:
The bubbling of the click event is extinguished.
Copy Code code as follows:

$ ("P"). Click (Function (event) {
Event.stoppropagation ();
Do something
});


Things are not difficult, mainly to record the blocking event bubbling.

Because the DIV adds a click event, the img inside the div also adds a click event, so when you click IMG, it triggers the Click event on the IMG, triggering the Click event on the Div, which is the event bubbling.

We can easily stop him in jquery.

As follows

Copy Code code as follows:
Event.stoppropagation ();

So click IMG no longer triggers the DIV's Click event.

Copy Code code as follows:

$ (' div '). Click (function () {
var $div = $ (this);
if ($div. FIND (' img '). Size () > 0} {
return;
}else{
$div. css (' backgroundcolor ', ' #e1f0f3 ');
$ (' ').
Appendto ($ (this)). Click (Function (event) {
$div. css (' backgroundcolor ', ' #ffffff ');
$ (this). Remove ();
Event.stoppropagation ();
}). css (' margin-left ', ' 10px ');
}
});

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.