e.target 可以用于按钮吗

发布于 2024-09-28 21:32:21 字数 302 浏览 4 评论 0原文

我没有使用 MouseEvent 分配给每个按钮,而是通过以下方式分配给 AIR 应用程序:

private function init():void {
  this.addEventListener(MouseEvent.MOUSE_DOWN,mpressKey);
}

但是,我只希望 mouse_down 在检测到“button”属性而不是 Demo0.WindowedApplicationSkin2.Group3.contentGroup.g4 时执行>(g4 是一个 id)。

Rather than assign to every buttons with MouseEvent, I assign to AIR application with:

private function init():void {
  this.addEventListener(MouseEvent.MOUSE_DOWN,mpressKey);
}

However, I only want the mouse_down to execute if it detect a "button" property instead of Demo0.WindowedApplicationSkin2.Group3.contentGroup.g4 (g4 is an id).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稳稳的幸福 2024-10-05 21:32:21

不要依赖 event.target 来检查按钮是否被单击。目标属性设置为单击的最里面的项目。当您单击某个按钮时,您并不总是单击该按钮;而是单击该按钮。您可能会单击显示标签的文本字段,或背景图像(如果有),或其他一些子皮肤部分等 - target 将设置为此内部项目。

如果您希望所有按钮都有一个单击处理程序,并根据单击的按钮采取适当的操作,您可以为每个按钮分配与处理程序相同的函数并检查 event.currentTarget 属性;当调用事件处理程序时,currentTarget 将设置为注册该处理程序的对象。

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);
btn3.addEventListener(MouseEvent.CLICK, clickHandler);

public function clickHandler(e:MouseEvent):void
{
  if(e.currentTarget == btn1){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn2){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn3){
    /* Handle btn1 here */
  }
}

当您使用 airApp.addEventListener 添加单个鼠标处理程序时,currentTarget 将始终是您的 airApp,因此您无法使用它来执行操作作为一个函数来处理所有这些。

Don't rely on event.target to check if a button was clicked or not. The target property is set to the innermost item that was clicked on. When you click on a button, you're not always clicking on The Button; you might be clicking on the text field that displays the label, or the background image if any, or some other child skinning part etc - the target will be set to this inner item.

If you want to have a single click handler for all buttons and take appropriate action based on the button clicked, you can assign same function as handlers for each button and check the event.currentTarget property; when an event handler is invoked, currentTarget is set to the object with which that handler was registered.

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);
btn3.addEventListener(MouseEvent.CLICK, clickHandler);

public function clickHandler(e:MouseEvent):void
{
  if(e.currentTarget == btn1){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn2){
    /* Handle btn1 here */
  }
  else if(e.currentTarget == btn3){
    /* Handle btn1 here */
  }
}

When you add a single mouse handler using airApp.addEventListener, the currentTarget will always be your airApp and thus you can't use it to act as a single function to handle them all.

小伙你站住 2024-10-05 21:32:21

您是否想知道如何测试目标是按钮还是特定按钮?

如果它是一个按钮

if ( e.target is Button ) { ... }

或者它是一个特定的按钮

if ( e.target == myButton ) { ... }

Are you asking how to test to see if the target is a Button, or a particular button?

If it's a Button

if ( e.target is Button ) { ... }

or if it's a particular button

if ( e.target == myButton ) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文