我正在尝试为CustomEvent制作一个polyfill,它也支持IE8,这是我想到的:
if (typeof window.CustomEvent === "function") {
return false;
} else {
function CustomEvent (eventName, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
if (document.createEvent) { // IE9, IE10
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(
eventName,
params.bubbles,
params.cancelable,
params.detail
);
return evt;
} else if (document.createEventObject) { // IE8
var evt = document.createEventObject();
evt.eventType = eventName;
}
};
CustomEvent.prototype = window.CustomEvent.prototype;
window.CustomEvent = CustomEvent;
}
对于IE8,我如何通过
params
? 最佳答案
您可以遍历params
并将它们分配给事件对象:
var evt = document.createEventObject();
evt.eventType = eventName;
for (var prop in params) {
evt[prop] = params[prop];
}