本文介绍了禁用Firefox愚蠢的右键单击上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个HTML 5游戏,需要使用右键单击来控制玩家。



我已经可以通过执行禁用右键单击上下文菜单:

 < body oncontextmenu =return(false);> 

然后我注意到,如果按住shift并右键单击,上下文菜单仍会打开Firefox!



所以我通过添加这个JS来禁用它:

  document.onclick = function(e){if(e.button == 2 || e.button == 3){e.preventDefault(); e.stopPropagation();返回(假); }}; 

但是,如果您按住shift键,然后在Firefox中右键双击,它仍会打开!



请告诉我如何彻底禁用这个血腥的事情(我甚至愿意回复一些模糊不清,不切实际的解决方案,只要它有效)。

解决方案

在所有情况下,您将永远无法完全禁用上下文菜单,因为firefox的设置允许用户告诉浏览器忽略你试图拉动的这种hijinx。

注:我在Mac上,但这个设置在所有平台上的相同位置。



这就是说,尝试event.preventDefault()(参见Vikash Madhow对这个其他SO问题的评论:

I am making an HTML 5 game which requires the use of right click to control the player.

I have been able to disable the right click context menu by doing:

<body oncontextmenu="return(false);">

Then it came to my attention that if you hold shift and right click, a context menu still opens in Firefox!

So I disabled that by adding this JS as well:

document.onclick = function(e) { if(e.button == 2 || e.button == 3) { e.preventDefault(); e.stopPropagation(); return(false); } };

However, if you hold shift, and then double right click in Firefox it still opens!

Please tell me how to disable this bloody thing once and for all (I'm even willing to revert to some obscure, hacky, and unpractical solution, as long as it works).

解决方案

You will never be able to entirely disable the context menu in all cases, as firefox has a setting that allows the user to tell the browser to ignore such hijinx as you are trying to pull.Note: I'm on a mac, but this setting is in pretty uch the same place over all platforms.

That being said, try event.preventDefault() (see Vikash Madhow's comment on this other SO question:How to disable right-click context-menu in javascript)

这篇关于禁用Firefox愚蠢的右键单击上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 19:49
查看更多