Trigger right-click

橙三吉。 提交于 2019-12-17 18:55:05

问题


I am trying to late-bind context menus to elements, using the ContextMenu plugin. So on the first right-click on those elements, I would like to :

  1. intercept the right-click through a live event on a certain "uncontextmenued" class,
  2. determine if the data('events').contextmenu exists,
  3. if not, attach the context-menu (and change the class to avoid re-throwing this live process),
  4. re-throw the right-click event to show the right-click.

I'm having trouble with the last item. jQuery allows to .click() or to .trigger('click'), which simulate a left-click, but there seems not to be a way to fire a right-click event through trigger.

Or is there?


回答1:


You can trigger it by

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

http://api.jquery.com/trigger/#example-5




回答2:


There is a newer way to do this:

$('#element').triggerHandler('contextmenu');

Documentation can be found here.




回答3:


Similar to this, but I'm not sure if you may be referring to jQuery UI data, but.

$('#element').mousedown(function(event) 
{
    if(event.which == 3)
    {
        if(typeof($(this).data('events')) === 'undefined')
        {
            $(this).data('events', { somedata: 'hello' });
        }
        else
        {
            // "re-throw" right click context menu
        }
    }
});


来源:https://stackoverflow.com/questions/6250447/trigger-right-click

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