我正在尝试为支持IE8的CustomEvent制作一个polyfill,这就是我提出的:
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
?
答案 0 :(得分:1)
您可以迭代params
并将它们分配给事件对象:
var evt = document.createEventObject();
evt.eventType = eventName;
for (var prop in params) {
evt[prop] = params[prop];
}