IE中不支持CustomEvent()
构造函数。是否可以使其至少与IE11兼容?它与其他浏览器如Chrome和Firefox一起工作。
例如:-
var SpecialEvent = new CustomEvent(
"SpecialMessage",
{
detail:
{
message: "Hello There",
time: new Date()
},
bubbles: true,
cancelable: true
});
最佳答案
MDN为IE>=9提供了一个polyfill。见下文。
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
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;
})();