MouseEvent无法在Internet Explorer中运行

Cha*_*ato 18 javascript internet-explorer

那是我吗?这是我的IE吗?或者为什么这段代码不适用于IE 11:

var clicker = new MouseEvent("click", {
  'bubbles': true,
  'cancelable': true,
  'view': window,
  'detail': 0,
  'screenX': 0,
  'screenY': 0,
  'clientX': 0,
  'clientY': 0,
  'ctrlKey': false,
  'altKey': false,
  'shiftKey': false,
  'metaKey': false,
  'button': 0,
  'relatedTarget': null
});
Run Code Online (Sandbox Code Playgroud)

我在控制台上得到"对象不支持此操作"(F12).我不得不提出一个解决方法,但我只是不明白为什么以前的代码不起作用(顺便说一句,以前的代码来自这里:https://msdn.microsoft.com/en-us/library /ie/dn905219 ( v=vs.85).aspx(创建和解雇合成事件).解决方法:

if (typeof MouseEvent !== 'function') {
    (function (){
        var _MouseEvent = window.MouseEvent;
        window.MouseEvent = function (type, dict){
            dict = dict || {};
            var event = document.createEvent('MouseEvents');
            event.initMouseEvent(
                    type,
                    (typeof dict.bubbles == 'undefined') ? true : !!dict.bubbles,
                    (typeof dict.cancelable == 'undefined') ? false : !!dict.cancelable,
                    dict.view || window,
                    dict.detail | 0,
                    dict.screenX | 0,
                    dict.screenY | 0,
                    dict.clientX | 0,
                    dict.clientY | 0,
                    !!dict.ctrlKey,
                    !!dict.altKey,
                    !!dict.shiftKey,
                    !!dict.metaKey,
                    dict.button | 0,
                    dict.relatedTarget || null
            );
            return event;
        }
    })();
}
Run Code Online (Sandbox Code Playgroud)

这笔交易是我想尽可能将已弃用的createEvent/initXXXXEvent迁移到新表单(var event = new XXXXEvent(...)),而不是依赖于已弃用的方法.

Rom*_*las 11

您提供的链接中的MSDN文档指示DOM L4事件构造函数模式的新语法:

适用于Windows 10 Technical Preview及更高版本的Internet Explorer.

这是与您使用的IE不同的版本.所以预计IE11不支持此功能

  • 这是一个解决方法:http://www.codeproject.com/Tips/893254/JavaScript-Triggering-Event-Manually-in-Internet-E (8认同)
  • 啊! 我的大脑想要阅读IE 10 ...与Windows 10的IE完全不同.感谢你指出这个问题. (3认同)

Dmi*_*try 5

有一个polyfill使其可以在IE中使用。

除了该try catch代码中的块需要如下所示:

try {
  new CustomEvent('test');
  return false; // No need to polyfill
} catch (e) {
  // Need to polyfill - fall through
}
Run Code Online (Sandbox Code Playgroud)

我将此更正提交到他们的网站。