新事件('build')vs new CustomEvent('build') [英] new Event('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 thedetail
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屋!