how to simulate right click in javascript [duplicate]

孤者浪人 提交于 2019-12-03 15:22:08

with jQuery

$('#recover').trigger({
    type: 'mousedown',
    which: 3
});

otherwise

var element = document.getElementById('recover');
var e = element.ownerDocument.createEvent('MouseEvents');

e.initMouseEvent('contextmenu', true, true,
     element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
     false, false, false,2, null);


return !element.dispatchEvent(e);

Sure, you can use the jQuery trigger() functionality.

$('#recover').trigger({
    type: 'mousedown',
    which: 3
});

Depending on what you're doing, you may wish to trigger a mouse down and then a mouse up, which could go like this:

$('#recover').trigger({
    type: 'mousedown',
    which: 3
}).trigger({
    type: 'mouseup',
    which: 3
});

I'm not a big fan of chaining long commands like that, but whatever is most readable for your app is fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!