简体   繁体   English

使用JavaScript将onrightclick属性添加到画布

[英]Add onrightclick property to canvas with JavaScript

I am trying to add onrightclick property to my canvas like this: 我试图将onrightclick属性添加到我的画布,如下所示:

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

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

The code is simple and I do the same for 'onclick' and 'onrightclick'. 代码很简单,我对'onclick'和'onrightclick'做同样的事情。 The onclick part is working, but when I right click I get browser right click. onclick部分正在运行,但是当我右键单击时,我右键单击浏览器。 How can I override the browser right click as intended? 如何按预期覆盖浏览器右键?

probably you're looking for oncontextmenu 可能你正在寻找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 示例小提琴(在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 如果你想使用dojo小部件使用上下文菜单,那么请查看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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