是有可能确定哪些对象调用基于事件侦听器的功能?例如,我有舞台上2个按钮调用相同功能被点击时,他们。我想的函数,以确定哪个按钮被发送者。

firstButton.addEventListener(MouseEvent.CLICK, myFunction);
secondButton.addEventListener(MouseEvent.CLICK, myFunction);

function myFunction(e:MouseEvent):void
 {
 var myString:String = "The button that called this function was (??)";
 trace(myString);
 }
有帮助吗?

解决方案

使用属性 currentTarget当前从 事件

function myFunction(e:MouseEvent):void {
 var myString:String = "The button that called this function was "+e.currentTarget;
 trace(myString);
}

其他提示

内myfunction的,e.currentTarget应保持到发送该事件的按钮的引用。

我只想补充到:在你的功能得到了事件对象有两个属性,有时可能会比较混乱区分它们:

e.target - 目标总是会回到你该事件的原始调度,所以如果你点击了一个按钮,但如果听父母你的目标仍然是按钮。

e.currentTarget - “的对象,该对象正在使用某个事件侦听器处理事件对象”。currentTarget当前将返回从事件或砖坯语言提出对象

所以你currentTarget当前和目标可以产生不同的效果,你需要记住这一点。 附:如果事件不冒泡(非可视/非用户交互鼠标事件),那么你currentTarget当前和目标将是相同的,或者如果你问,在冒泡事件的情况下,调度的事件的对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top