jquery three ways to cancel event bubbling (recommended)

Source: Internet
Author: User

1. Cancel the default behavior by returning false and prevent the event from bubbling.

JQuery Code:

?
1234567891011 $("form").bind(  "submit",    function() {      return false;    });

2. Only the default behavior is canceled by using the Preventdefault () method.

JQuery Code:

?
123456789 $("form").bind(  "submit",   function(event){    event.preventDefault();  });

3. Only one event bubble is blocked by using the Stoppropagation () method.

JQuery Code:

?
123456789 $("form").bind(  "submit",   function(event){    event.stopPropagation();  });

(4) Summary

1. One event blistering corresponds to the same event that triggered the upper layer

Special: If both are set to double-click events, the event that triggers one click is bubbled when you click

(Double-click to include the clicked).

2. If in the Click event, precede the event you want to process with e.preventdefault ();

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

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

?
1234567891011 //如果提供了事件对象,则这是一个非IE浏览器if ( e && e.stopPropagation )  // 因此它支持W3C的stopPropagation()方法  e.stopPropagation();else  //否则,我们需要使用IE的方式来取消事件冒泡  window.event.cancelBubble = true;returnfalse;

jquery three ways to cancel event bubbling (recommended)

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.