编程语言
首页 > 编程语言> > javascript – MouseEventConstructor不是构造函数

javascript – MouseEventConstructor不是构造函数

作者:互联网

当我在本地执行测试时,它们没有任何问题,但是当在服务器上进行测试时,我得到:

TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown',
EXEC : error : TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown', 
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        })')

代码:

HTMLElement.prototype.mouseDownLeftButton = function () {
        var event = new MouseEvent('mousedown',
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
        this.dispatchEvent(event);
    };

这完全没问题.有没有其他方法来创建一个新的MouseEvent?

解决方法:

很可能你的本地测试infra使用真正的浏览器,而在服务器上它使用PhantomJS.

后者仍然不支持新的MouseEvent:https://github.com/ariya/phantomjs/issues/11289

我必须做以下技巧才能通过测试:

    function createMouseEvent(typeArg: string): MouseEvent {
        let event = document.createEvent('MouseEvent');
        event.initMouseEvent(typeArg,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined);
        return event;
    }

标签:qunit,javascript,constructor,mouseevent
来源: https://codeday.me/bug/20190727/1552498.html