javascript 键盘事件上的 preventDefault() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14055096/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:35:37  来源:igfitidea点击:

preventDefault() on keyup event not working

javascriptjquerygoogle-chromeinternet-explorer-8

提问by Solo Omsarashvili

I cannot get preventDefault()to work.

我无法preventDefault()上班。

Here are some different code variations I have tried:

以下是我尝试过的一些不同的代码变体:

First:

第一的:

$(document).keyup(function (evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode == 192) {
        alert('192');
        return false;
    }
});

Second:

第二:

$(document).keyup(function (evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode == 192) {
        alert('192');
        evt.preventDefault();
    }
});

Third:

第三:

$(document).keyup(function (evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode == 192) {
        alert('192');
        evt.preventDefault();
        return false;
    }
});

Only alert works.
Everything works on Opera,but not on Chrome and IE Also tried keydownand keypress. On keypressscript doesn't work. $('#thetext').keydown(function(evt){});neither works this.
Here is whole code: http://bbullett.tk/test/kakey.html

只有警报有效。
一切都适用于 Opera,但不适用于 Chrome 和 IE 也尝试过keydownkeypress. 在keypress脚本上不起作用。 $('#thetext').keydown(function(evt){});两者都不起作用。
这是整个代码:http: //bbullett.tk/test/kakey.html

Key 192 = `

I'm trying to insert some text or symbol instead of `

我正在尝试插入一些文本或符号而不是 `

回答by undefined

Listening to keyupevent is too late for calling preventDefault, try listening to keypressor keydowninstead.

监听keyup事件来不及调用preventDefault,尝试监听keypresskeydown代替。

$('input[type=text], textarea').on('keydown', function(event){
    if (event.which == 192) {
        console.log('192');
        event.preventDefault();
    }
});

Note that jQuery normalizes whichproperty and it's cross-browser.

请注意,jQuery 规范了which属性并且它是跨浏览器的。

http://jsfiddle.net/763ys/

http://jsfiddle.net/763ys/

回答by Stuart Burrows

There is no default behaviour for .keyup(); it fires after the key press which triggers the default behaviour. Use .keydown() or.keypress()` instead.

没有默认行为.keyup();它在触发默认行为的按键后触发。改用.keydown() or.keypress()`。

source:http://api.jquery.com/category/events/keyboard-events/

来源:http : //api.jquery.com/category/events/keyboard-events/

回答by samjudson

You are using JQuery, which normalises the differences between browsers, so you only need to to check the evt.which property. See the JQuery documentation: http://api.jquery.com/keyup/

您正在使用 JQuery,它规范了浏览器之间的差异,因此您只需要检查 evt.which 属性。请参阅 JQuery 文档:http: //api.jquery.com/keyup/

As others have said - keyup is too late to stop the processing of the key. Use keydown or keypress instead. http://api.jquery.com/category/events/keyboard-events/

正如其他人所说 - keyup 来停止处理密钥为时已晚。请改用 keydown 或 keypress。http://api.jquery.com/category/events/keyboard-events/

回答by ameya rote

see working example here http://jsfiddle.net/WGSvm/

在此处查看工作示例http://jsfiddle.net/WGSvm/

In all three examples you have used

在您使用的所有三个示例中

$(document).keyup(function (evt) {  });

I would say use particular div id instead of $(document).keyup(function (evt) { });

我会说使用特定的 div id 而不是 $(document).keyup(function (evt) { });

like $("#DivIdName").keyup(function (evt) { });and also use onkeypressstrictly, because if you use onkeyupit has already executed its default task of printing or whatever it is.

喜欢$("#DivIdName").keyup(function (evt) { });onkeypress严格使用,因为如果你使用onkeyup它已经执行了它的默认打印任务或其他任何任务。