在开始之前,我并不是要询问如何阻止用户单击鼠标右键。这是用于在 html5 Canvas 上进行右键单击检测。
所以目前,我所拥有的是:
function init() {
var canvas = document.getElementById('canvasID');\
if (canvas.getContext) {
ctx = canvas.getContext('2d');
ctx.font = '10px sans-serif';
ctx.canvas.onmousedown=ctx.canvas.onclick=onMousePressed;
ctx.canvas.onmouseup=ctx.canvas.onmouseout=onMouseReleased;
ctx.canvas.onmousemove = onMouseMove;
}
}
还有:
function onMouseMove(e) {
if (mouseDown) {
click2 = convertCoordsToTileCoords({
x: e.offsetX || e.layerX - this.style.left,
y: e.offsetY || e.layerY - this.style.top
});
var rightclick;
if (!e) e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e.button == 2);
..等等其中变量“rightclick”是一个 bool 值,如果用户右键单击则结果为 true。
适用于哪些浏览器: Chrome
Opera,不,因为他们的鼠标手势和东西。 IE,不,因为 Canvas 。 但我不明白为什么它不能在 Firefox 上运行。
像往常一样,如果有更好、更有效的方法来解决这个问题,我们将不胜感激。
-大副
Edit1:提供了更多代码。
最佳答案
如果您不介意使用 jQuery,可以使用 Right-click Plugin这让事情变得简单多了。
关于javascript - Firefox 的右键单击检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3383547/