JavaScript事件起泡与捕获

时间:2023-03-09 09:36:04
JavaScript事件起泡与捕获
// 向 <div> 元素添加事件句柄
document.getElementById("myDIV").addEventListener("mousemove", myFunction); // 移除 <div> 元素的事件句柄
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);

addEventListener() 方法用于向指定元素添加事件句柄。

element.addEventListener(event, function, useCapture)  //语法

useCapture:可选。布尔值,指定事件是否在捕获或冒泡阶段执行。

  • true - 事件句柄在捕获阶段执行
  • false- false- 默认。事件句柄在冒泡阶段执行

事件捕获阶段capture phase,传播顺序:window -> 事件源的父元素.  The event object must propagate through the target's ancestors from the Window to the target's parent.

事件冒泡阶段bubble phase,传播顺序:事件源父元素 -> window.  The event object propagates through the target’s ancestors in reverse, starting with the target's parent and ending with the Window.

目标阶段target phase:事件对象到达事件源。The event object arrive at the event object's event target.

可参考相关文章 : https://www.cnblogs.com/zhuzhenwei918/p/6139880.html

http://blog.csdn.net/qq_28602957/article/details/60475123