跳至主要內容

JavaScript 阻止冒泡及默认行为

h7ml小于 1 分钟

JavaScript 阻止冒泡及默认行为

阻止冒泡

  • e.cancalBubble = true:IE8- chrome
  • e.stopPropagation():firefox chrome

兼容:

e.stopPropagation ? e.stopPropagation() : (e.cancalBubble = true);

阻止默认行为

  • 普通函数: return false
  • addEventListener:e.preventDefault() // chrome
  • attachEvent:e.returnValue = false // IE

兼容:

e.preventDefault ? e.preventDefault() : (e.returnValue = false);

阻止 IE 选择事件

  • onmousedown:setCapture() 设置捕获
  • onkeydown:releaseCapture() 释放捕获