其他分享
首页 > 其他分享> > 阻止事件冒泡兼容代码

阻止事件冒泡兼容代码

作者:互联网

//兼容代码
function stopPropagation(e) {
  e = e || window.event
  if(e.stopPropagation) { //w3c 阻止事件冒泡
    e.stopPropagation()
  }else{                  //IE 阻止事件冒泡
    e.cancelBubble = true
  }
}

//事件调用兼容代码
document.getElementById('dv2').onclick = function(e) {
  stopPropagation(e)
  console.log(2)
}

标签:function,代码,兼容,事件,stopPropagation,冒泡
来源: https://blog.csdn.net/qq_44777678/article/details/101073732