简体   繁体   中英

Add onrightclick property to canvas with JavaScript

I am trying to add onrightclick property to my canvas like this:

canvas.onclick = function(evnt) {
    doSomethingClick();
}

canvas.onrightclick = function(evnt) {
    doSomethingRightClick();              
}

The code is simple and I do the same for 'onclick' and 'onrightclick'. The onclick part is working, but when I right click I get browser right click. How can I override the browser right click as intended?

probably you're looking for oncontextmenu

<canvas id="cnv" width="200" height="200"></canvas>

var cnv = document.getElementById('cnv');
cnv.oncontextmenu = function() {
   alert('right click');  
   return false; 
}

example fiddle (tried on Fx12/MacOS) : http://jsfiddle.net/Jk3Jx/1

dojo.connect(canvas, 'onclick', function(e) {
  if (dojo.mouse.isRight(e)) {
    ...
  }
});

If you want a context menu using, dojo widgets, then check out

http://dojotoolkit.org/reference-guide/1.7/dijit/Menu.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM