Block event bubbling

Source: Internet
Author: User

Previously know that there are events bubbling, but has been encountered, today the revision program finally met. Now summarize the points

1: What is called event bubbling

There are 2 kinds of events in the JS time processing mechanism, capturing event events and bubbling events. The bubbling event is the same event that starts from the event source and then calls the previous layer, knowing the Window object. Capturing events happens to be the opposite.

2: Several ways to block event bubbling:

A: Returns false directly at the end of the event function in jquery

$ ("form"). Bind ("Submit", function () {return false; } );

  

B: Original JS blocking event bubbling

function Stopeventbubble (event) {          var e=event | | window.event;         if (e && e.stoppropagation) {                  e.stoppropagation ();                    } else{                 e.cancelbubble=true;              }      }

  

C: Block Browser default events

var el = window.document.getElementById ("a");    El.onclick = function (e) {        //If an event object is provided, this is a non-ie browser if        (e && e.preventdefault) {            //block default browser action (web) C4/>e.preventdefault ();        }            the way to block the default action of a function in the else {//ie             window.event.returnValue = false;            return false;        }    }

  

Block event bubbling

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.