How JS prevents bubbling events and default events

1 Overview

Preventing bubbling and default events are both very important in js. In fact, it is very simple. Only one or two sentences are required. The key is to be compatible!

2 Stop bubbling events

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

3 Block default events

function stopDefault(e){
    
      
  if(e && e.preventDefault) {
    
      
    e.preventDefault();  
  } else {
    
      
    window.event.returnValue = false;  
  }  
  return false;  
}; 

Guess you like

Origin blog.csdn.net/qq_41800366/article/details/102754627