开发者

How can I trigger right click if you left click on some html element using jQuery?

Basically, I want to open a context m开发者_运维问答enu on left click itself. Is there anyway to do this using jQuery?


You can't. JavaScript does not have that access to the browser. Instead you could create your own custom context menu and try to give it the behavior choices you want from the normal context menu (Back, forward, etc). Of course, some of those may be restricted (like copy/paste).

http://labs.abeautifulsite.net/projects/js/jquery/contextMenu/demo/


you can record event for right click and trigger whatever event you want to perform on right click.


You can't trigger the right click, but you can trigger a keypress using .trigger()

Shift + F10 should trigger the context menu on Windows, something like...

function openContextMenu() {
  jQuery.event.trigger({ type: 'keypress', which: 121, shiftKey: true });
}

Also there's a context-menu key (on the right before CTRL on 104+ key keyboards) that I think might be keycode 93:

function openContextMenu() {
  jQuery.event.trigger({ type: 'keypress', which: 93 });
}

Update

Actually these just simulate the event - any JS events for that event fire, but the actual key doesn't get sent.

You can do this with an ActiveX object:

// ActiveX object
var shell = new ActiveXObject("WScript.Shell");

// Send SHIFT+F10
shell.SendKeys("+{F10}");

However that component is marked as not safe for scripting and is IE only, so that solution is only really practical for intranets and the like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜