Javascript 创建新MouseeEvent外观的传统方式有多过时?
我读到了这篇文章:在旧版本中,他们在以旧的方式呈现时会提到一些细节 我有一些密码:Javascript 创建新MouseeEvent外观的传统方式有多过时?,javascript,javascript-events,Javascript,Javascript Events,我读到了这篇文章:在旧版本中,他们在以旧的方式呈现时会提到一些细节 我有一些密码: HTMLElement.prototype.mouseDownLeftButton = function () { var event = 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);
};
在某些环境中无法正确执行。因此,我用老式的方式写了一个版本:
HTMLElement.prototype.mouseDownLeftButton = function () {
var event = document.createEvent("MouseEvent");
event.initEvent("mousedown", true, false);
event.setAttribute("which", 1);//how to do it correctly?
event.setAttribute("view", window);//how to do it correctly?
this.dispatchEvent(event);
};
但不幸的是,我得到了
TypeError:event.setAttribute不是函数
问题:如何正确设置
哪个到1
和查看到窗口
?要澄清哪个:1
表示鼠标左键已“按下”。您需要使用event.currentTarget.setAttribute
并仅使用event.which
属性,该属性将检测
1 - Left
2 - Middle
3 - Right
JS
谢谢,但我得到:无法读取null的属性“setAttribute”
@Yoda是否将事件附加到了某个东西上?是的,但事件。哪个
和事件。查看
刚刚帮了忙。但我需要检查其他环境。
HTMLElement.prototype.mouseDownLeftButton = function () {
var event = document.createEvent("MouseEvent");
event.initEvent("mousedown", true, false);
event.setAttribute("which", event.which);//how to do it correctly?
event.setAttribute("view", window);//how to do it correctly?
this.dispatchEvent(event);
};