如何使用javascript在IE9中触发Enter键事件?

时间:2018-06-12 13:25:58

标签: javascript javascript-events internet-explorer-9 element

我需要使用JS在IE9中触发Enter Key Press事件。就像下面适用于Chrome的代码一样

var evsat = document.createEvent('Event');
evsat.initEvent('keypress');
evsat.keyCode = 13;
elem.dispatchEvent(evsat);
IE中不支持

createEvent(' Event'),抛出

"Object doesn't support property or method 'createEvent'" 

请建议我其他一些解决方法。

更新:如何在下面的代码中处理ENTER事件?

https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

(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;
})();

0 个答案:

没有答案