jQUery context menu on left click

2019-05-18 15:35发布

问题:

I am using jQuery context menu plugin by Chris Domigan to appy a context menu. This is how it works:

$('#contacts tbody tr').contextMenu('myMenu1', {
    bindings: {
        'copy': function(t) {
             alert('Trigger was '+t.id+'\nAction was Copy');
         },

        'delete': function(t) {
             alert('Trigger was '+t.id+'\nAction was Delete');
        }
    },             
});

Now I want this context menu to appear on left click instead of right click. I can not find an option in the doc. Any ideas how to do that? Do I have to modify the source?

回答1:

As the title on the source

ContextMenu - jQuery plugin for right-click context menus

Also in the doc there's no info about how to change the kind of click. I think the only thing you could do is to extend that code to work with the basic "click()" too ;)



回答2:

I know it's old, but i'll answer it anyways ;)

If you want to call ContextMenu on left-click, just change the line:

$(this).bind('contextmenu', function(e) {

into this:

$(this).bind('click', function(e) {

But if You want to capture more events to display ContextMenu, You can add event names after spacebar, according to jQuery .bind() reference.

For instance, if You want to display the menu on left and right click just change the line into this:

$(this).bind('contextmenu click', function(e) {


回答3:

It looks like you would need to change the code. You need to change this line:

 $(this).bind('contextmenu', function(e) {

into this

 $(this).bind('click', function(e) {


回答4:

Matthew Manela, thanks for your snippet (spent hours at this point)

Also, in my project I want to support both right and left click. (Maybe someone would need that) To do this you change your code to:

$(this).bind('click contextmenu', function(e) {


回答5:

i know that this is far too late for me to say this.

new contextjs plugin using 'on' instead of bind, because 'on' can define selector like this:

$(document)on('contextmenu','selector',function(e){

i use this to define my 'left' or 'right' :

if(type === 'right'){
    $(document).on('contextmenu', selector, function (e) {
    ....}
else{
    $(document).on('click', selector, function (e) {
    ....}