了解路由事件:为什么我需要气泡事件和隧道事件?
I read this good article about Routed Events, and I understood why we need sometimes bubble Events and sometime we need tunnel Events.
What I didn't understand is, when we use the tunnel Event, why after it is handled or reached to the source element, still the bubble event is launched?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该文章表示,如果您将图像放在按钮上,然后单击该图像,则将触发该图像的隧道事件。但您很可能还希望像单击按钮本身一样处理该单击,因此还会触发一个冒泡事件,该事件会冒泡到按钮本身的单击事件(因为按钮拥有图像),因此您可以使用按钮的常用事件处理程序将图像单击视为按钮单击。
The article says that if you put an image on a button, and that image is clicked, the tunnel event for that image will fire. But it is highly likely that you would also want to handle that click as if the button itself was clicked, so a bubble event is also fired which bubbles up to the click event on the button itself (because the button owns the image), so that you can treat the image click like a button click, using the usual event handler for the button.
我还想补充一点,两者(隧道和气泡)的事件是不同的。
对于隧道,我们使用 PreviewXXX(例如:PreviewMouseDown),对于气泡事件,我们使用 XXX(例如:MouseDown)事件。
按顺序,Tunnel 事件首先从应用程序的根开始触发,并在源处结束。并触发 Bubble 事件。
举办这两项活动是非常有意义的。
I would also like to add that the event for both (Tunnel and Bubble) are different.
For tunnel, we use PreviewXXX (eg: PreviewMouseDown) and for bubble event we use XXX (eg: MouseDown) event.
By sequence, Tunnel event gets fired first starting from the root of the application and ends at the source. And the Bubble event is triggered.
It makes perfect sense to have both these events.