JavaScript implements an example of the interrupt propagation and Behavior Blocking method for an event, and a javascript example

Source: Internet
Author: User

JavaScript implements an example of the interrupt propagation and Behavior Blocking method for an event, and a javascript example

Event Propagation

MicroSoft's design is that when an event is triggered on an element, the event is then triggered on the parent node of the node, and so on. The event is continuously propagated along the DOM tree, until the document element of the top-level object is reached. This bottom-up event propagation method is called "event bubbling", that is, event propagation.

How can we interrupt the spread of events?

stopPropagation()W3c cancel bubble

cancleBubble = trueIE cancels bubbling

Default Effect of canceling an event:

returnValue = falseIE cancel Event Effect

defaultPrevent()W3c event cancellation effect

<div id='aa'> <div id='bb'> <div id ='cc'></div> </div></div>
 #aa{ width: 600px; height: 600px; background: gray;}#bb{ width: 400px; height: 400px; background: green;}#cc{ width: 200px; height: 200px; background: red;}

Capture writing stop propagation start from top to bottom

Document. getElementById ('A '). addEventListener ('click', function (ev) {alert ('A'); ev. stopPropagation () ;}, true); // The result is captured by aa plus true and changed from bubble to capture document from top to bottom. getElementById ('bb '). addEventListener ('click', function () {alert ('bb ')}, true); document. getElementById ('cc '). addEventListener ('click', function () {alert ('cc')}, true );

Bubble writing stops propagation from bottom up

Document. getElementById ('A '). addEventListener ('click', function () {alert ('A') ;}); // Add true to change from bubble to capture document from top to bottom. getElementById ('bb '). addEventListener ('click', function () {alert ('bb')}); document. getElementById ('cc '). addEventListener ('click', function (ev) {alert ('cc'); ev. stopPropagation (); // ev. cancleBubble = true; // cancel the bubbling method under IE}); // The result is that cc stops propagation}

Cancel Event Effect

returnValue = false// IE cancels the Event Effect

preventDefault()// W3c cancel Event Effect

Document. getElementsByTagName ('A') [0]. onclick = function (ev) {alert ('click'); // The event ends, but the function continues to run. // ev. preventDefault (); alert ('intercept ');}

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.