gpt4 book ai didi

javascript - MouseEventConstructor 不是构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:36 24 4
gpt4 key购买 nike

当我在本地执行测试时,它们毫无问题地通过了,但是当测试在服务器上进行时,我得到:

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

这完全没问题。还有其他方法可以创建 new MouseEvent 吗?

最佳答案

MDN 中有一个 polyfill 可以解决这个问题:

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent#Polyfill

(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);

关于javascript - MouseEventConstructor 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com