Block event of cancels the browser's default behavior for the event and prevents it from propagating

  • 2020-03-26 21:45:06
  • OfStack

Disables the browser's default behavior (response) for events (e.g. < A> Tag jump, etc.) and stop the event from propagating.

The implementation code
 
function stopEvent (evt) { 
var evt = evt || window.event; 
if (evt.preventDefault) { 
evt.preventDefault(); 
evt.stopPropagation(); 
} else { 
evt.returnValue = false; 
evt.cancelBubble = true; 
} 
} 

Only prevent the event from spreading (do not cancel the default behavior)
 
function stopEvent (evt) { 
var evt = evt || window.event; 
if (evt.stopPropagation) { 
evt.stopPropagation(); 
} else { 
evt.cancelBubble = true; 
} 
} 

Only cancels the default behavior (does not prevent the event from propagating)
 
function stopEvent (evt) { 
var evt = evt || window.event; 
if (evt.preventDefault) { 
evt.preventDefault(); 
} else { 
evt.returnValue = false; 
} 
} 

Related articles: