我不明白有什么区别,它们看起来是一样的,但我想它们不是。
任何关于何时使用其中一个或另一个的例子都将是非常感谢的。
我不明白有什么区别,它们看起来是一样的,但我想它们不是。
任何关于何时使用其中一个或另一个的例子都将是非常感谢的。
当前回答
我喜欢直观的答案。
当您单击#btn时,将调用两个事件处理程序,它们将输出您在图片中看到的内容。
演示在这里:https://jsfiddle.net/ujhe1key/
其他回答
. currenttarget总是返回事件监听器被添加到的组件。
另一方面,e.target可以是组件本身,也可以是接收事件的任何直接子代、孙子代、孙子代等等。换句话说,e.target返回位于Display List层次结构顶部的组件,该组件必须位于子层次结构或组件本身中。
一种用途是当你在Canvas中有几个图像,你想在组件中拖动图像。你可以在Canvas上添加一个监听器,在监听器中你可以编写以下代码来确保Canvas不会被拖拽。
function dragImageOnly(e:MouseEvent):void
{
if(e.target==e.currentTarget)
{
return;
}
else
{
Image(e.target).startDrag();
}
}
本的回答完全正确,所以要记住他说的话。我要告诉你的不是一个完整的解释,但这是一个非常简单的方法来记住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”。
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.
e.currentTarget是注册事件的元素(父),e.target是事件指向的节点(子)。
E.target是元素,你可以点击它 currenttarget是添加了事件监听器的元素。
如果你点击按钮的子元素,最好使用currentTarget来检测按钮属性,在CH中使用e.target有时会有问题。