disable all keyup/keydown/keypressed events presen

2020-08-17 05:35发布

问题:

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.

Thanks!!

回答1:

You could do 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 keypress events that are bound to them.

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

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>).

Take a look at this working demo.



回答2:

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).



回答3:

you can use preventDefault() function to solve it



回答4:

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



回答5:

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');



回答6:

$(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.jstree being bound with a timeout of 500ms.

Simply adding the above off binding 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

Hope this helps.



回答7:

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

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