为什么 IE9+ 上的 Web 浏览器不支持 document.createEvent 以及如何修复它



我正在使用Windows 8,Internet Explorer 10,Visual Studio 2013

这是javascript代码:

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;
    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }
    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
    if (true || document.createEvent) // temporary it's always true to check IE
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}
function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}
var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
};

我的网页 :

<a href="http://google.com" id="rrx"  > google </a>

这是我调用模拟的javascript代码:

simulate(document.getElementById('rrx'), "click"); 

问题是我没有被重定向到谷歌网页,因为IE抛出了一个异常,并显示不支持document.createEvent的消息。据我所知,IE9+应该支持document.createEvent

添加警报以显示document.documentModedocument.compatMode,您看到了什么?

我相信你应该在注册表中同时具有正确的FEATURE_BROWSER_EMULATION值,在HTML本身中具有<!DOCTYPE html>值,以便所有现代HTML DOM/JavaScript功能正常工作。

这也适用于您的相关问题。

相关内容

最新更新