我不明白有什么区别,它们看起来是一样的,但我想它们不是。

任何关于何时使用其中一个或另一个的例子都将是非常感谢的。


当前回答

本的回答完全正确,所以要记住他说的话。我要告诉你的不是一个完整的解释,但这是一个非常简单的方法来记住e.target和e. currentarget是如何与鼠标事件和显示列表相关的:

目标=鼠标下面的东西(如本所说…触发事件的东西)。 . currenttarget =点前面的东西…(见下文)

因此,如果你在一个实例名为“btns”的剪辑中有10个按钮,你会:

btns.addEventListener(MouseEvent.MOUSE_OVER, onOver);
// btns = the thing before the dot of an addEventListener call
function onOver(e:MouseEvent):void{
  trace(e.target.name, e.currentTarget.name);
}

e.target将是10个按钮之一,e.currentTarget将始终是“btns”剪辑。

值得注意的是,如果您将MouseEvent更改为ROLL_OVER或设置属性btns。mouseChildren to false, e.target和e. currentarget都将总是“btns”。

其他回答

E.target是元素,你可以点击它 currenttarget是添加了事件监听器的元素。

如果你点击按钮的子元素,最好使用currentTarget来检测按钮属性,在CH中使用e.target有时会有问题。

举个例子:

var body = document.body,
    btn = document.getElementById( 'id' );
body.addEventListener( 'click', function( event ) {
    console.log( event.currentTarget === body );
    console.log( event.target === btn );
}, false );

当你点击'btn'时,会出现'true'和'true' !

我喜欢直观的答案。

当您单击#btn时,将调用两个事件处理程序,它们将输出您在图片中看到的内容。

演示在这里:https://jsfiddle.net/ujhe1key/

target  is the element that triggered the event (e.g., the user clicked on)

currenttarget is the element that the event listener is attached to.

那件事值得注意。Target可能很有用,例如,用于使用单个侦听器来触发不同的操作。假设你有一个典型的“菜单”精灵,里面有10个按钮,所以不要这样做:

menu.button1.addEventListener(MouseEvent.CLICK, doAction1);
menu.button2.addEventListener(MouseEvent.CLICK, doAction2);
etc...

你可以简单地做:

menu.addEventListener(MouseEvent.CLICK, doAction);

并根据事件在doAction(event)中触发不同的操作。目标(使用它的name属性,等等…)