0

例として keydown イベントリスナーを取り上げます。

使用できます

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

keydown イベントが発生すると、上記のイベント リスナーが実行されることを理解しています。

次のような「afterkeydownイベント」が必要です。

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

ただし、キーが繰り返し保持され、タイマーが遅い場合、キーアップイベントリスナーはトリガーを取得しないため、これを実現するためにキーアップイベントリスナーや任意の種類のタイマーを使用したくありません。

これらの制限の下でキーダウンの「コールバックイベントリスナー」は可能ですか?もしそうなら、すべてのJqueryイベントリスナーに一般化できますか? つまり、「afterclick」、「afterdrop」、「afterclick」、... Jquery のイベントリスナーを作成することは可能ですか?

4

1 に答える 1

0

あなたが実際にやろうとしているのは、テキストエリアが変更されたときに通知を受けることだと思われるので、その特定の問題に使用したコードを次に示します。キーボード、ドラッグ/ドロップ、マウスなどでコンテンツが変更されるたびに、コールバックが呼び出されます...

(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);
于 2013-08-05T06:56:51.613 に答える