Compatibility handling of events in JS

1. Get the even and target objects

var e = event || window.event;
var target = e.target || e.srcElement;

2. Stop the event from bubbling up

function clearEventBubble() {
    
    
var e = event || window.event;
  // e.stopPropagation(); 标准浏览器
  // e.cancelBubble = true; IE浏览器
  e.stopPropagation && e.stopPropagation() || (e.cancelBubble = true);
}

3. Prevent the default behavior of the event

function stopDefault() {
    
    
var e = event || window.event;
  // e.preventDefault(); 标准浏览器
  // e.returnValue = false; IE浏览器
  e.preventDefault && e.preventDefault() || (e.returnValue = false); // IE浏览器
}

Guess you like

Origin blog.csdn.net/qq_26705343/article/details/114091673