Javascript MouseEventConstructor不是构造函数
当我在本地执行测试时,它们会顺利通过,但当测试在服务器上进行时,我会得到:Javascript MouseEventConstructor不是构造函数,javascript,constructor,mouseevent,qunit,Javascript,Constructor,Mouseevent,Qunit,当我在本地执行测试时,它们会顺利通过,但当测试在服务器上进行时,我会得到: TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown', EXEC : error : TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown',
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使用real browser,并且在服务器上使用PhantomJS 后者仍然不支持新的MouseeEvent: 我必须执行以下技巧才能通过测试:
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;
}
MDN的polyfill将解决此问题:
“本地”、“服务器上”-请更具体地说明所使用的环境。听起来好像根本没有DOM。@Bergi的意思是,当我在本地运行qUnit测试时,一切正常,但当服务器作为连续集成的一部分运行它们时,它们会因为这个原因而失败。我不知道服务器本身的配置。是否有比新的MouseeEvent更旧的版本语法可用?关于“无DOM”,RHS并不是LHS,所以我认为DOM是完整的。谢谢。键盘活动有这样的填充物吗?“我对那个也有问题。@兹拉特科,你们为KeyboardEvent弄到了polyfill吗?”?挣扎于same@Ankit不,我想我没有。
(function (window) {
try {
new MouseEvent('test');
return false; // No need to polyfill
} catch (e) {
// Need to polyfill - fall through
}
// Polyfills DOM4 MouseEvent
var MouseEvent = function (eventType, params) {
params = params || { bubbles: false, cancelable: false };
var mouseEvent = document.createEvent('MouseEvent');
mouseEvent.initMouseEvent(eventType, params.bubbles, params.cancelable, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
return mouseEvent;
};
MouseEvent.prototype = Event.prototype;
window.MouseEvent = MouseEvent;
})(window);