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 :
data('events').contextmenu
exists,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?
We will use the oncontextmenu property to listen to the right-click events. We will create a rightClick() function. In this function, we will call the preventDefault() method of the mouse click event which will cancel the default behavior of the right-click event. We can also return “false” to cancel the event.
The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.
Set the window.contextmenu property to an event handler function to listen for right clicks on the page. We have the e parameter that has the right-click event object. We call e. preventDefault to stop the default behavior for right clicks, which is to show the context menu on the page.
You can detect which button has been clicked by checking the MouseEvent. button property. This property will be available on mouse clicking events like: mousedown , mouseup , click , dblclick , contextmenu . The MouseEvent.
You can trigger it by
$('#element').trigger({
type: 'mousedown',
which: 3
});
http://api.jquery.com/trigger/#example-5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With