如何为 IE11 的 customevent 转换 dispatchEvent?

How to convert dispatchEvent for a customevent for IE11?

下面这个功能在 IE11 上不起作用,因为 IE11 不支持 dispatchEvent 和 CustomEvent。

如何转换此函数以使其在 IE11 上运行?

如果浏览器是 IE11,我尝试添加一个 returns true/false 的变量,并基于此禁用 dispatchEvent 代码。但这没有用。

关于如何让此代码在 IE11 上运行的任何想法?

 function disableLoader(dataElement) {
    const dataElement = document[0].querySelector('[data-id]');
    const dataElementId = dataElement.getAttribute('data-id');

    dataElement.dispatchEvent(new CustomEvent('dataLoaded', {
        detail: {
            dataId: dataElementId,
            status: 'finished'
        }
    }));
}

您的问题似乎已在此处得到解答:IE 11 DispatchEvent。 您也可以使用像这样的 polyfill https://github.com/Hooked74/events-polyfill. As for passing details, you need to call the yourevent.initCustomEvent() method passing the details object as your last argument before dispatching as described by its documentation: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/initCustomEvent.

您可以使用以下代码在 Internet Explorer 9 及更高版本中填充 CustomEvent() 构造函数功能:

    (function () { 
        if (typeof window.CustomEvent === "function") return false; 
        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;
    })();

更多详细信息,请查看CustomEvent() constructor and this sample code。示例输出如下: