为什么document.createEvent不与web浏览器IE9上的+和如何解决它的支持? [英] Why document.createEvent is not supported with webbrowser on IE9+ and how to fix it?

查看:270
本文介绍了为什么document.createEvent不与web浏览器IE9上的+和如何解决它的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Windows 8, Internet Explorer 10, Visual Studio 2013

下面是JavaScript代码:

Here is javascript code :

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



我的HTML:

My HTML :

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

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

And here is my javascript code that calls simulate :

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

的问题是,因为IE抛出一​​个异常,并留言我不是重定向到谷歌网页 document.createEvent 不支持。
据我所知 document.createEvent 应与IE9 +支持。

The problem is that I'm not redirected to google web page because IE throws a exception with message that document.createEvent is not supported. As far as I know document.createEvent should be supported with IE9+.

推荐答案

添加警报显示 document.documentMode document.compatMode ,你怎么看?

Add an alert to show document.documentMode and document.compatMode, what do you see?

我相信你应该同时拥有正确的 FEATURE_BROWSER_EMULATION 在注册表中和值< !DOCTYPE HTML> 在HTML本身,所有现代的HTML DOM / JavaScript的功能进行了介绍。

I believe you should have both the correct FEATURE_BROWSER_EMULATION value in the registry and <!DOCTYPE html> in the HTML itself, for all modern HTML DOM/JavaScript features to work.

这也适用于你的相关问题

这篇关于为什么document.createEvent不与web浏览器IE9上的+和如何解决它的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆