ActionScript 3 是否有某种事件委托系统?
我有一个装有许多图像的容器。我不想在每个图像上添加单击和其他鼠标事件的侦听器,而是只想侦听图像父级上的这些事件。
这可能吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,那是完全有可能的。因此,假设您有类似的内容:
那么您应该能够将事件侦听器绑定到父级:
然后您的处理程序应该看起来像这样
您还可以将其与 addEventListener 的 useCapture 参数结合起来进行附加事件发生在事件的捕获端而不是冒泡端。并且还可以在事件上使用
.stopPropagation()
方法来阻止任何其他事件处理程序触发...但是很难说您是否需要在不了解更多信息的情况下使用这些方法试图做。但希望这会推动你朝着正确的方向前进。
If i am understanding your question correctly, that is totally possible. So assuming you have something like:
Then you should be able to bind the event listener to the parent thusly:
and then your handler should look something like
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.