JS simple way to prevent bubbling events and default events from occurring

  • 2020-03-30 01:24:25
  • OfStack

If < P> In < Div> Inside, what about that? < P> There is an onclick event, < Div> There are also onclick events to trigger < P> If the click event of the parent element is not triggered, then the following function needs to be called:


function stopBubble(e){
  if(e&&e.stopPropagation){//The IE
   e.stopPropagation();
  }
  else{//IE
   window.event.cancelBubble=true;
  }
 } 

< A onclick = '> If you want to prevent the default event from triggering, which is the default href event, you need to call the following function:


function stopDefault( e ) {
         //Block default browser action (W3C)
         if ( e && e.preventDefault )
             e.preventDefault();
         //The way to block the default action of the function in IE
         else
             window.event.returnValue = false;
         return false;
     } 


Related articles: