克隆DOM事件对象以重新调度 [英] Clone a DOM event object to re-dispatch

查看:71
本文介绍了克隆DOM事件对象以重新调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些浏览器不允许您重新调度已经调度的事件,但允许您基于可以从现有事件对象获得的值来创建新的事件对象。

Some browsers won't allow you to re-dispatch an event that has already been dispatched, but allow you to create new event objects based on values that can be obtained from the existing event object.

是否存在适用于任何事件类型的通用且可重用的解决方案,否则将无法针对特定事件类型进行处理(在我的情况下,我目前关注的是 mousewheel 事件)?

Is there a generic and reusable solution that will work with any event type, or failing that, a way to do this for a specific event type (in my case I'm currently concerned with the mousewheel event)?

推荐答案

似乎现在有了更好的解决方案,因为不推荐使用 initMouseEvent 之类的东西。例如, MouseEvent()构造函数属性表作为其第二个参数,可以使用其现有的MouseEvent对象:

It seems there is now an even better solution, since initMouseEvent and the like are deprecated. The MouseEvent() constructor, for example, takes a table of properties as its second parameter, for which you can use an existing MouseEvent object:

let my_event = new MouseEvent(`foo`, some_existing_mouse_event);
dispatchEvent(my_event);

其他类的事件具有相似的构造函数,应以相同的方式使用。例如 ClipboardEvent()

Other classes of events have similar constructors that should be usable in the same way. Such as ClipboardEvent().

jsfiddle示例

这篇关于克隆DOM事件对象以重新调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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