CustomEvent IE8支持,如何传递params?

时间:2016-03-29 15:37:47

标签: javascript javascript-events internet-explorer-8 custom-events

我正在尝试为支持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

1 个答案:

答案 0 :(得分:1)

您可以迭代params并将它们分配给事件对象:

var evt = document.createEventObject();
evt.eventType = eventName;

for (var prop in params) {
  evt[prop] = params[prop];
}