简体   繁体   English

JS-一旦选择了输入字段,请禁用Eventlistener

[英]JS - Disable Eventlistener once input field is selected

I'm currently adding a EventListener to my document. 我目前正在将EventListener添加到我的文档中。 Which works great using the following code: 使用以下代码效果很好:

document.addEventListener('keydown', function(event) 
{
    if(event.keyCode === 68){
        socket.emit('keyPress', {inputId: 'right', state: true});
    }
    else if(event.keyCode === 83){
        socket.emit('keyPress', {inputId: 'down', state: true});
    }
    else if(event.keyCode === 65){
        socket.emit('keyPress', {inputId: 'left', state: true});
    }
    else if(event.keyCode === 87){
        socket.emit('keyPress', {inputId: 'up', state: true});
    }
});

document.addEventListener('keyup', function(event) 
{
    if(event.keyCode === 68){
        socket.emit('keyPress', {inputId: 'right', state: false});
    }
    else if(event.keyCode === 83){
        socket.emit('keyPress', {inputId: 'down', state: false});
    }
    else if(event.keyCode === 65){
        socket.emit('keyPress', {inputId: 'left', state: false});
    }
    else if(event.keyCode === 87){
        socket.emit('keyPress', {inputId: 'up', state: false});
    }
});

But once I would select a input field and type something containing W, A, S, D then the EventListener would be triggered. 但是一旦我选择了一个输入字段并输入包含W,A,S,D的内容,就会触发EventListener。

Is there a way to disable the eventListener once the input field is selected? 一旦选择了输入字段,是否可以禁用eventListener?

You need to disable event bubbling from input. 您需要从输入中禁用事件冒泡。 Something like this: 像这样:

input.addEventListener('keyup', function (e) {

  e.stopPropagation();

}, false);
input.addEventListener('keydown', function (e) {

  e.stopPropagation();

}, false);

So using code above you will disable keyup and keydown event bubbling and no one parent node of the input will not hear these events 因此,使用上面的代码,您将禁用keyupkeydown事件冒泡,并且输入的任何父节点都不会听到这些事件

Yes check what event.target is, if it is your input element just do a return, if not then let the code continue on. 是的,请检查event.target是什么,如果它是您的输入元素,则只需返回即可,否则请继续执行代码。

document.addEventListener('keydown', function(event) {
  if(event.target === someReferenceToYourElement){
     return;
  }
  //rest of code
});

You could add an event listener on the focus of the input. 您可以在输入焦点上添加事件侦听器。

document.getElementsByTagName('input').addEventListener('focus', function(e) {
    e.StopPropagation();
}

I suggest to you to do something like that: 我建议您做这样的事情:

$('document').keypress(function(event) {
    if (event.which == 65) { // A
        // Do your stuff
    }
})

This code is the same as your but on the entier DOM. 此代码与您的代码相同,但在整个DOM上。

$('#input').val.indexOf('a')

This code will check if your field contains the letter A, can be the string 'abcd' or 'bar', etc. 此代码将检查您的字段是否包含字母A,可以是字符串“ abcd”或“ bar”,等等。

If you want to check when the field loose the focus you can also you the event blur of jquery https://api.jquery.com/blur/ 如果您想检查字段何时失去焦点,也可以查看jquery https://api.jquery.com/blur/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM