新事件('build')vs new CustomEvent('build') [英] new Event('build') vs new CustomEvent('build')

查看:74
本文介绍了新事件('build')vs new CustomEvent('build')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此MDN页面上创建和触发事件它显示了使用 Event CustomEvent 创建事件的示例。它解释了 CustomEvent 允许自定义详细信息,但除此之外,它没有多说。

On this MDN page Creating and Triggering Events it shows an example of creating event with Event or CustomEvent. It explains that CustomEvent allows for custom details, but other than that, it doesn't say much.

所以, 有什么不同?如果我正在创建一个通用滚动事件,我应该使用 CustomEvent 吗?或者仅适用于javascript中不存在的事件?

So, what is the difference? If I'm creating a generic scroll event, should I use CustomEvent? Or is it only for events not exist in javascript?

此外,我注意到 MouseEvent 也是事件,所以如果我正在创建点击事件,我可以使用 new MouseEvent('click')

Also, I noticed that MouseEvent is also a child of Event, so if I'm creating a click event, I can just use new MouseEvent('click')?

谢谢

推荐答案

从该指南:


要向事件对象添加更多数据, CustomEvent 界面存在且 detail 属性可用于传递自定义数据。

To add more data to the event object, the CustomEvent interface exists and the detail property can be used to pass custom data.

您可以使用任何事情的事件()。如果要附加自定义数据,请使用 CustomEvent('eventName',{data})

You can use Event() for anything. If you want to attach custom data, you use CustomEvent('eventName', {data}).

是的,对于鼠标事件,你应该使用 MouseEvent

And yes, for mouse-events, you should use MouseEvent.

这篇关于新事件('build')vs new CustomEvent('build')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆