2013-08-05 94 views
0

我将以keydown事件监听器为例。Afterevent事件监听器

一个可以使用的

$('#my-input').keydown(function(){ 
    //actions goes here 
}); 

我能理解上述事件侦听器,当它遇到一个keydown事件中运行。

我需要一个“afterkeydown事件”,例如:

$('#my-input').afterkeydown(function(){ 
    //actions after keydown listener has occured 
}); 

然而,我不希望使用keyup事件监听器,也没有任何形式的计时器来实现这一 因为keyup事件侦听器没有得到触发当一个键被重复地拿着,定时器很慢时。

在这些限制条件下,keydown上是否存在“回调事件侦听器”,如果有的话,是否可以推广到所有Jquery事件侦听器?意思,是否有可能创建一个'afterclick','afterdrop','afterclick',... jquery的事件监听器?

+0

什么afterclick点,afterdrop等。 ..?我没有看到任何理由。 – jfriend00

+0

假设您正在尝试创建一个插件,用于自动更正textarea中的用户输入。 – wklhuey

+2

keyup?你能描述一下你想做什么吗? – Sergio

回答

0

因为它听起来像你真正想做的事情是随时随地通知一个textarea被更改,这里有一些我用于特定问题的代码。它会调用你的回调随时内容已通过键盘,拖/放,鼠标等被改变......

(function($) { 

    var isIE = false; 
    // conditional compilation which tells us if this is IE 
    /*@cc_on 
    isIE = true; 
    @*/ 

    // Events to monitor if 'input' event is not supported 
    // The boolean value is whether we have to 
    // re-check after the event with a setTimeout() 
    var events = [ 
     "keyup", false, 
     "blur", false, 
     "focus", false, 
     "drop", true, 
     "change", false, 
     "input", false, 
     "textInput", false, 
     "paste", true, 
     "cut", true, 
     "copy", true, 
     "contextmenu", true 
    ]; 
    // Test if the input event is supported 
    // It's too buggy in IE so we never rely on it in IE 
    if (!isIE) { 
     var el = document.createElement("input"); 
     var gotInput = ("oninput" in el); 
     if (!gotInput) { 
      el.setAttribute("oninput", 'return;'); 
      gotInput = typeof el["oninput"] == 'function'; 
     } 
     el = null; 
     // if 'input' event is supported, then use a smaller 
     // set of events 
     if (gotInput) { 
      events = [ 
       "input", false, 
       "textInput", false 
      ]; 
     } 
    } 

    $.fn.userChange = function(fn, data) { 
     function checkNotify(e, delay) { 
      var self = this; 
      var this$ = $(this); 

      if (this.value !== this$.data("priorValue")) { 
       this$.data("priorValue", this.value); 
       fn.call(this, e, data); 
      } else if (delay) { 
       // The actual data change happens aftersome events 
       // so we queue a check for after 
       // We need a copy of e for setTimeout() because the real e 
       // may be overwritten before the setTimeout() fires 
       var eCopy = $.extend({}, e); 
       setTimeout(function() {checkNotify.call(self, eCopy, false)}, 1); 
      } 
     } 

     // hook up event handlers for each item in this jQuery object 
     // and remember initial value 
     this.each(function() { 
      var this$ = $(this).data("priorValue", this.value); 
      for (var i = 0; i < events.length; i+=2) { 
       (function(i) { 
        this$.on(events[i], function(e) { 
         checkNotify.call(this, e, events[i+1]); 
        }); 
       })(i); 
      } 
     }); 
    } 
})(jQuery);  

用法为:

$("#whatever").userChange(yourCallbackFn, optionalData); 
+0

我比寻找用户输入的东西更感兴趣的是寻找事后监听者。此外,这个解决方案有一个计时器,这不是我想要的。 – wklhuey

+0

@wklhuey - 没有这样的事情作为事后的听众,所以你最好寻找解决你的实际问题(这是我试图提供)。计时器是触发事件的唯一方法,事件监听器由其他代码处理后,当您希望键盘处理程序已完成其工作时,通常是您想要的。 – jfriend00