0

オプションで 1 つまたは複数の値を渡すことができる新しい jQuery 関数を作成したいと思います。次に、DOM のすべての一致する要素について、すべての異なるオプションを繰り返しますが、一度に 1 つのオプションを繰り返します。したがって、処理されなかったすべての要素について、プロトタイプ関数を再生します。すべての処理は同じ関数内にあるはずなので、再帰で考えましたが、javascript でのプロトタイピングの経験があまりないため、それを行う方法がわかりません。今のところ、エラーはありませんが、何も起こりません。

あれは正しいですか ?

あなたの啓発に感謝します。

(function($) {
    $.fn.foo = function (prop) {
        var options = $.extend({
            "elt" : "",   // Declaration of my options
            "callback" : function(){}
        }, prop)

        var optionsTab = options.elt.split('#');
        var current = optionsTab.slice(0,1),
        var remaining = optionsTab.slice(1);
        var result = this.each(function() {
            var el = $(this);
            // Code handling the first element in the option
        });

        if (remaining.length > 0) {
            $({ "elt": remaining.join("#") }); // Trial of recursion of foo
            return result;
        }
    }
4

2 に答える 2

0

タイムアウトで再帰的に呼び出そうとしていると思いますが、正しいですか?

    var result = this.each(function() {
        var el = $(this);
        // Code handling the first element in the option
    });

    if (remaining.length > 0) {
        $({ "elt": remaining.join("#") }); // Trial of recursion of foo
       var me = this;
       setTimeout(function(){
         me.foo(parameters);//not sure what the parameters should be
       },500);
    }
    return this;

役立つ情報: https://stackoverflow.com/a/16063711/1641941 (「この変数」の下)

于 2013-11-10T13:21:43.320 に答える