如何在元素上发送双击事件?

时间:2012-07-23 16:01:17

标签: javascript javascript-events

我可以发送一个简单的点击事件(没有任何可选参数):

var clickEvt = document.createEvent("MouseEvents");
clickEvt.initEvent("click");
element.dispatchEvent(clickEvt);

但是如何发送双击事件?

3 个答案:

答案 0 :(得分:4)

只需将"click"替换为"dblclick"中的clickEvt.initEvent

var dblclickEvt = document.createEvent("MouseEvents");
dblclickEvt.initEvent("dblclick");
element.dispatchEvent(dblclickEvt);

答案 1 :(得分:2)

接受的答案(// Returns an empty array, therefore evaluates to true if (result.declinedPermissions) { throw new Error('Permissions were declined') } // The first index of the array is empty if (result.declinedPermissions[0] === "") { throw new Error('Permissions were declined') } )中使用的特征是deprecated

相反,请使用Event()

等特定事件构成器

所以回答你的问题,就像是:

Event.initEvent()

答案 2 :(得分:0)

这适用于Chrome @ 83

const event = new MouseEvent('dblclick', {
  bubbles: true,
  cancelable: true,
  view: window
});

const buttonEl = document.querySelector('#my-button');
buttonEl.dispatchEvent(event);