Javascript 取消 HTML 中的 keydown
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3036243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cancel the keydown in HTML
提问by Hyman
How can I cancel the keydownof a specific key on the keyboard, for example(space, enter and arrows) in an HTML page.
如何取消keydown键盘上特定键的 ,例如arrowsHTML 页面中的(空格、回车和)。
回答by Tim Down
If you're only interested in the example keys you mentioned, the keydownevent will do, except for older, pre-Blink versions of Opera (up to and including version 12, at least) where you'll need to cancel the keypressevent. It's much easier to reliably identify non-printable keys in the keydownevent than the keypressevent, so the following uses a variable to set in the keydownhandler to tell the keypresshandler whether or not to suppress the default behaviour.
如果您只对您提到的示例键感兴趣,则该keydown事件将起作用,除了较旧的、Blink 之前的 Opera 版本(至少包括版本 12),您需要取消该keypress事件。在keydown事件中可靠地识别不可打印的键比keypress事件容易得多,因此以下使用在keydown处理程序中设置的变量来告诉keypress处理程序是否抑制默认行为。
var cancelKeypress = false;
document.onkeydown = function(evt) {
evt = evt || window.event;
cancelKeypress = /^(13|32|37|38|39|40)$/.test("" + evt.keyCode);
if (cancelKeypress) {
return false;
}
};
/* For Opera */
document.onkeypress = function(evt) {
if (cancelKeypress) {
return false;
}
};
回答by marcgg
Catch the keydown event and return false. It should be in the lines of:
捕获 keydown 事件并返回 false。它应该在以下几行:
<script>
document.onkeydown = function(e){
var n = (window.Event) ? e.which : e.keyCode;
if(n==38 || n==40) return false;
}
</script>
(见这里)
The keycodes are defined here
密钥代码在此处定义
edit: update my answer to work in IE
编辑:更新我的答案以在 IE 中工作
回答by Alexis J. Normandia
I only develop for IE because my works requires it, so there is my code for numeric field, not a beauty but works just fine
我只为 IE 开发,因为我的作品需要它,所以有我的数字字段代码,不是美,但工作得很好
$(document).ready(function () {
$("input[class='numeric-field']").keydown(function (e) {
if (e.shiftKey == 1) {
return false
}
var code = e.which;
var key;
key = String.fromCharCode(code);
//Keyboard numbers
if (code >= 48 && code <= 57) {
return key;
} //Keypad numbers
else if (code >= 96 && code <= 105) {
return key
} //Negative sign
else if (code == 189 || code == 109) {
var inputID = this.id;
var position = document.getElementById(inputID).selectionStart
if (position == 0) {
return key
}
else {
e.preventDefault()
}
}// Decimal point
else if (code == 110 || code == 190) {
var inputID = this.id;
var position = document.getElementById(inputID).selectionStart
if (position == 0) {
e.preventDefault()
}
else {
return key;
}
}// 37 (Left Arrow), 39 (Right Arrow), 8 (Backspace) , 46 (Delete), 36 (Home), 35 (End)
else if (code == 37 || code == 39 || code == 8 || code == 46 || code == 35 || code == 36) {
return key
}
else {
e.preventDefault()
}
});
});
回答by Neboj?a Gojni?
This is certainly very old thread.
In order to do the magic with IE10 and FireFox 29.0.1 you definitely must do this inside of keypress(not keydown) event listener function:
这当然是非常古老的线程。为了在 IE10 和 FireFox 29.0.1 上实现魔法,您绝对必须在keypress(非keydown)事件侦听器函数中执行此操作:
if (e.preventDefault) e.preventDefault();
回答by Luke Duddridge
jQuery has a nice KeyPress functionwhich allows you to detect a key press, then it should be just a case of detecting the keyvalue and performing an if for the ones you want to ignore.
jQuery 有一个很好的KeyPress 函数,它允许您检测按键按下,那么它应该只是检测键值并为您想要忽略的键值执行 if 的情况。
edit: for example:
编辑:例如:
$('#target').keypress(function(event) {
if (event.keyCode == '13') {
return false; // or event.preventDefault();
}
});
回答by redben
Just return false. Beware that on Opera this doesn't work. You might want to use onkeyup instead and check the last entered character and deal with it. Or better of use JQuery KeyPress
只是返回false。请注意,在 Opera 上这是行不通的。您可能想改用 onkeyup 并检查最后输入的字符并处理它。或者更好地使用 JQuery KeyPress

