useCapture在addEventListener的侦听器中

时间:2013-04-10 14:25:10

标签: javascript

我想在target.addEventListener(type,listener [,useCapture])的listener方法中确定useCapture的布尔值。有办法做到这一点还是可能?

target.addEventListener(type, listener[, useCapture]);
divs[i].addEventListener("click", listener, false);

function listener()
{
    //Determine the false of useCapture here;
}

2 个答案:

答案 0 :(得分:0)

If the listener is not passed with a different value for useCaputre, you can check if the listener is in the capture phase.

var useCapture = event.eventPhase === Event.CAPTURING_PHASE;

答案 1 :(得分:0)

There are three event phases:

  • The capture phase: the event object must propagate through the target's ancestors from the defaultView to the target's parent. This phase is also known as the capturing phase. Event listeners registered for this phase must handle the event before it reaches its target.

  • The target phase: the event object must arrive at the event object's event target. This phase is also known as the at-target phase. Event listeners registered for this phase must handle the event once it has reached its target. If the event type indicates that the event must not bubble, the event object must halt after completion of this phase.

  • The bubble phase: the event object propagates through the target's ancestors in reverse order, starting with the target's parent and ending with the defaultView. This phase is also known as the bubbling phase. Event listeners registered for this phase must handle the event after it has reached its target.

You can know the current phase through the eventPhase property of the event.

However, the problem is that there isn't a perfect correspondence:

  • If event.eventPhase is 1 (capturing phase), it means useCapture was true.
  • If event.eventPhase is 3 (bubbling phase), it means useCapture was false.
  • If event.eventPhase is 2 (at target), useCapture could have been either true or false.