阻止菜单键显示上下文菜单 [英] Prevent menu key from showing a context menu
本文介绍了阻止菜单键显示上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道:)
$(function(){
var lastKey = 0;
$(窗口).on(keydown,document,function(event){
lastKey = event.keyCode;
});
$(窗口).on(contextmenu,文档,函数(事件){
if(lastKey === 93){
lastKey = 0;
event.preventDefault();
event.stopPropagation();
返回false;
}
});
});
I know that the keyboard menu key is keyCode === 93
.
So I have the following code:
$(window).on("keydown", document, function(event){
if (event.keyCode === 93) { //context menu
console.log("context menu key", event);
event.preventDefault();
event.stopPropagation();
return false;
}
});
Although the event does fire, and the console
does get logged inside the if statement, but the context menu still shows even though both event.preventDefault();
and event.stopPropagation();
are present in my code.
Is there any way to prevent the menu from being displayed?
Demo for fiddling: http://jsfiddle.net/maniator/XJtpc/
For those of you who do not know what the "menu" key is:
解决方案
This is kind of dumb but it seems to work: http://jsfiddle.net/XJtpc/2/ :)
$(function(){
var lastKey=0;
$(window).on("keydown", document, function(event){
lastKey = event.keyCode;
});
$(window).on("contextmenu", document, function(event){
if (lastKey === 93){
lastKey=0;
event.preventDefault();
event.stopPropagation();
return false;
}
});
});
这篇关于阻止菜单键显示上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文