Javascript 带有自定义事件初始化的对象不支持此操作 IE9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14358599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:29:48  来源:igfitidea点击:

Object doesn't support this action IE9 with CustomEvent Initialization

javascriptinternet-explorerjavascript-eventscustom-event

提问by Idan Shechter

I am getting the following error in IE9:

我在 IE9 中收到以下错误:

"Object doesn't support this action".

“对象不支持此操作”。

There are various question about this, but mine is specifically for the following code:

关于这个有各种各样的问题,但我的是专门针对以下代码的:

 var myEvent = new CustomEvent("additem");

From my understanding, CustomEvent is supported in IE9 as a DOM manipulation command. This works fine in Chrome without any exception.

根据我的理解,在 IE9 中支持 CustomEvent 作为 DOM 操作命令。这在 Chrome 中运行良好,没有任何例外。

Anyone has this problem and know how to solve it? Thanks.

任何人都有这个问题并知道如何解决它?谢谢。

回答by Flunk

Afaik custom events are not supported in IE, only in normal browsers. I suggest using a javascript library that provides a browser independent implementation like Jquery's trigger: http://api.jquery.com/trigger/

IE 不支持 Afaik 自定义事件,仅在普通浏览器中支持。我建议使用一个 javascript 库,它提供了一个独立于浏览器的实现,比如 Jquery 的触发器:http: //api.jquery.com/trigger/

回答by Florin Dobre

You can use a javascript function to detect if the browser is IE11 or lower then apply the next polyfill:

您可以使用 javascript 函数来检测浏览器是否为 IE11 或更低版本,然后应用下一个 polyfill:

    (function () {
      function CustomEvent ( event, params ) {
        params = params || { bubbles: false, cancelable: false, detail: undefined };
        var evt = document.createEvent( 'CustomEvent' );
        evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
        return evt;
      };

      CustomEvent.prototype = window.Event.prototype;
      window.CustomEvent = CustomEvent;
    })();

The polyfill from above is taken from MDN: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

上面的 polyfill 取自 MDN:https: //developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

回答by Scott Jungwirth

Try this polyfill which doesn't replace a native (and functional) CustomEvent method.

试试这个 polyfill,它不会取代原生(和功能性)的 CustomEvent 方法。

(function () {
  try {
    new CustomEvent('test');
    return;
  } catch(e) {
    // ignore this error and continue below
  }

  function CustomEvent ( event, params ) {
    params = params || { bubbles: false, cancelable: false, detail: undefined };
    var evt = document.createEvent( 'CustomEvent' );
    evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
    return evt;
  };

  CustomEvent.prototype = window.Event.prototype;
  window.CustomEvent = CustomEvent;
})();

回答by Rex Hsu

The following polyfill would not replacce native CustomEvent(),
Partial source from: MDN CustomEvent():

以下 polyfill 不会替换原生 CustomEvent(),
部分来源:MDN CustomEvent()

(function () {

  if (typeof CustomEvent === 'function') { return; }

  function customEvent(event, params) {

      params = params || {bubbles: false, cancelable: false, detail: undefined};

      var evt = document.createEvent('CustomEvent');

      evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);

      return evt;
  }

  customEvent.prototype = window.Event.prototype;
  window.CustomEvent = customEvent;
})();