Internet Explorer 9, 10 & 11 Event constructor doesn't work

后端 未结 7 795

I am creating an event, so use the DOM Event constructor:

new Event(\'change\');

This works fine in modern browsers, however in Internet Ex

7条回答
  •  死守一世寂寞
    2020-11-28 22:07

    There's an IE polyfill for the CustomEvent constructor at MDN. Adding CustomEvent to IE and using that instead works.

    (function () {
      if ( typeof window.CustomEvent === "function" ) return false; //If not IE
    
      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;
    })();
    

提交回复
热议问题