11

これはjQuery 1.8で非推奨になり、jQuery 1.9で削除されtoggle(...)ました

同じタイプの機能を持つ代わりに、一般的に(jQuery 移行スクリプトを使用する以外に)何が使用できますか?toggle(fn, fn2);

関連する質問 (特定のケースについての質問):トグルの代わりに何を使用しますか?


機能が削除されたのではなく、カスタムのトグル機能を追加する機能だけであることは知っています (表示/非表示のデフォルト機能は別として)toggle()

4

1 に答える 1

20

簡単な実装は次のとおりです。

$.fn.toggleClick = function() {
    var methods = arguments;    // Store the passed arguments for future reference
    var count = methods.length; // Cache the number of methods 

    // Use return this to maintain jQuery chainability
    // For each element you bind to
    return this.each(function(i, item){
        // Create a local counter for that element
        var index = 0;

        // Bind a click handler to that element
        $(item).on('click', function() {
            // That when called will apply the 'index'th method to that element
            // the index % count means that we constrain our iterator between 0
            // and (count-1)
            return methods[index++ % count].apply(this, arguments);
        });
    });
};

として使用します

$('selector').toggleClick( function1, function2, ... );
于 2013-01-17T16:08:49.187 に答える