简体   繁体   English

如何执行绑定到事件的侦听器并排除绑定到执行中的不同事件的其余侦听器?

[英]How to execute a listener bound to an event and exclude the remaining listeners bound to different events from execution?

I have the following jQuery code: 我有以下jQuery代码:

$input.on('keyup', keyUpListener);

$input.on('input', inputListener);

// IE <= 8 fallback for input event.
$input[0].onpropertychange = function() {
    if (window.event.propertyName === "value") {                
        inputListener(window.event);
    }
};

$input is a jQuery input[type='text'] . $input是一个jQuery input[type='text']

Right now keyUpListener and inputListener are both executed when I type into the input or when I copy and paste something ( onpropertychange is not fired because it is an IE only event). 现在keyUpListenerinputListener都是在我input或者当我复制并粘贴某些内容时执行的( onpropertychange未被触发,因为它是仅IE事件)。

But how can I tell JS to not fire inputListener if keyUpListener is executing and vice-versa? 但是,如果keyUpListener正在执行,我如何告诉JS不要触发inputListener ,反之亦然?

Attach the specific event to the textbox? 将特定事件附加到文本框?

$("#input").on('input', function () {
}

For instance the above - this way you can have multiple listeners not executing on the same input 例如上面 - 这样你就可以让多个监听器不在同一个输入上执行

EDIT 编辑

You are able to bind the paste function to the textbox also.. 您还可以将粘贴功能绑定到文本框..

$("#input").bind('paste', function() {
var pasted = true;
}); 

Thus you can then have the below IF statement: 因此,您可以使用以下IF语句:

if (pasted) {
$input.on('input', inputListener);
} else {
$input.on('keyup', keyUpListener);
}

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

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