如果EventConstructor不是构造函数,我应该如何创建事件? [英] If EventConstructor is not a constructor, how should I create the event?

查看:155
本文介绍了如果EventConstructor不是构造函数,我应该如何创建事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在追踪 MDN 指南,并尝试创建一个活动:

I'm following MDN guide and trying to create an event:

创建事件的MDN指南

var jGp = new Object();
...
jGp.evt = new Object();
jGp.evt.erro = new Event("jGp_evtErro");

错误是(Safari):

The error is (Safari):

TypeError: '[object EventConstructor]' is not a constructor (evaluating 'new Event("jGp_evtErro")')

还要使用 document.createEvent 接缝将被弃用(如 MDN 说),那么我应该如何创建我的活动?

Also to use document.createEvent seams to be deprecated (as MDN says), so how should I create my event?

推荐答案

我也在Safari上碰到过, / catch语句,以便尽可能使用不被废弃的构造函数,但如果需要,则以旧方式失败。

I ran into this too on Safari, I used a try/catch statement in order to use the non-deprecated constructor whenever possible, but fail into the old way if necessary.

jGp.evt = new Object();
try {
  jGp.evt.erro = new Event("jGp_evtErro");
}
catch (e) {
  jGp.evt.erro = document.createEvent('Event');
  jGp.evt.erro.initEvent("jGp_evtErro", true, true);
}

这篇关于如果EventConstructor不是构造函数,我应该如何创建事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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