ActionScript 3 是否有某种事件委托系统?

发布于 2024-12-26 02:59:36 字数 77 浏览 2 评论 0原文

我有一个装有许多图像的容器。我不想在每个图像上添加单击和其他鼠标事件的侦听器,而是只想侦听图像父级上的这些事件。

这可能吗?

I have a container with many images. Instead of adding a listener for click and other mouse events on each of the images, I would like to only listen for those events on the parent of the image.

Is that possible?

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

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

发布评论

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

评论(2

恬淡成诗 2025-01-02 02:59:36
container.addEventListener(MouseEvent.CLICK, clickHandler);
private function clickHandler(e:MouseEvent):void {
  trace(e.currentTarget); // references container
  trace(e.target); //references container's child or container itself depending on what has been clicked 
}
container.addEventListener(MouseEvent.CLICK, clickHandler);
private function clickHandler(e:MouseEvent):void {
  trace(e.currentTarget); // references container
  trace(e.target); //references container's child or container itself depending on what has been clicked 
}
雨落□心尘 2025-01-02 02:59:36

如果我正确理解你的问题,那是完全有可能的。因此,假设您有类似的内容:

parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());

那么您应该能够将事件侦听器绑定到父级:

parent.addEventListener(MouseEvent.CLICK, handleClick);

然后您的处理程序应该看起来像这样

private function handleClick(e:MouseEvent) {
    // cast the target of the event as the correct class
    var clickedChild:Child = Child(e.target); 

    // Do whatever you want to do.
}

您还可以将其与 addEventListener 的 useCapture 参数结合起来进行附加事件发生在事件的捕获端而不是冒泡端。并且还可以在事件上使用 .stopPropagation() 方法来阻止任何其他事件处理程序触发...

但是很难说您是否需要在不了解更多信息的情况下使用这些方法试图做。但希望这会推动你朝着正确的方向前进。

If i am understanding your question correctly, that is totally possible. So assuming you have something like:

parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());

Then you should be able to bind the event listener to the parent thusly:

parent.addEventListener(MouseEvent.CLICK, handleClick);

and then your handler should look something like

private function handleClick(e:MouseEvent) {
    // cast the target of the event as the correct class
    var clickedChild:Child = Child(e.target); 

    // Do whatever you want to do.
}

You can also combine this with the useCapture argument of addEventListener to attach the event on the capturing side of the event rather than the bubbling side. And also use the .stopPropagation() method on the Event to stop any other event handlers from firing as well...

But its difficult to say if you need to use those without knowing more about what you are trying to do. But hopefully that will give you a push in the right direction.

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