jQuery 禁用页面上存在的所有 keyup/keydown/keypressed 事件

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

disable all keyup/keydown/keypressed events present on page

javascriptjquery

提问by Milind Anantwar

I am having plenty of key events here on my page.Is there any way to disable all the keyup/keydown/keypressed events present on page rather than disabling each event separately. i am looking for solution using javascript/jquery.

我的页面上有很多关键事件。有没有办法禁用页面上存在的所有 keyup/keydown/keypressed 事件,而不是单独禁用每个事件。我正在寻找使用 javascript/jquery 的解决方案。

Thanks!!

谢谢!!

回答by Anthony Grist

You coulddo it this way, though I expect it might be horrendously slow on larger pages:

可以这样做,但我预计它在较大的页面上可能会非常慢:

$('*').off('keyup keydown keypress');

That's going to select every single element on the page, then remove any keyup, keydown, and keypressevents that are bound to them.

这就是要去选择页面上的每一个元素,然后取出所有keyupkeydown以及keypress绑定到这些事件。

If you want to prevent the user from using the backspacekey to navigate to the previous page, you could try the following code:

如果您想阻止用户使用该backspace键导航到上一页,您可以尝试以下代码:

var inputTags = ['INPUT', 'TEXTAREA'];

$(document).on('keypress', function(e) {
    if(e.which === 8 && $.inArray(e.target.tagName, inputTags) === -1)
        e.preventDefault();
});

That should restrict the use of the backspace key, except in instances where the focus is an input element where you can enter text (so an <input type="text">or a <textarea>).

这应该限制退格键的使用,除非在焦点是可以输入文本的输入元素的情况下(例如 an<input type="text">或 a <textarea>)。

Take a look at this working demo.

看看这个工作演示

回答by alexbusu

Try

尝试

$(document).find('*').off('keyup keydown keypressed');

and you should put this into the $(document).ready()block, after all the loaded JS on page (before </body>tag, for example).

并且您应该将其放入$(document).ready()块中,在页面上所有加载的 JS 之后(</body>例如,在标签之前)。

回答by Masoud

you can use preventDefault() function to solve it

您可以使用 preventDefault() 函数来解决它

回答by Stuart Grant

$('*').unbind('keyup keydown keypress')

$('*').unbind('keyup keydown keypress')

回答by Nole

For me worked this combination:

对我来说,这种组合是:

Turn on keypress event

开启按键事件

$(document).keypress(function(){
// Code
});

and for turning off this event I have used:

为了关闭这个事件,我使用了:

$(document).off('keypress');

回答by 4levels

$(elem).off('keypress.jstree')

$(elem).off('keypress.jstree')

I recently struggled with this as well since I was adding editing features to tree nodes. By looking at the source, I noticed there's an event keypress.jstreebeing bound with a timeout of 500ms.

我最近也在为此苦苦挣扎,因为我向树节点添加了编辑功能。通过查看源代码,我注意到有一个事件keypress.jstree被绑定了 500 毫秒的超时。

Simply adding the above offbinding after initializing the tree solved all my issues at once! This works both for static tree structures as well as tree's loaded with ajax data. The version I'm currently using is 3.3.5

只需off在初始化树后添加上述绑定即可立即解决我的所有问题!这既适用于静态树结构,也适用于加载了 ajax 数据的树。我目前使用的版本是 3.3.5

Hope this helps.

希望这可以帮助。

回答by Viviane Abitbol

This code deletes the last key pressed in TextBox2, by using substr.

此代码使用 substr 删除在 TextBox2 中按下的最后一个键。

 `$("#TextBox2").val($("#TextBox2").val().substr(0, ($("#TextBox2").val().length-1)));`
  //removes last letter pressed.