0

私はjqueryプラグインの連鎖性に取り組んでいますが、以下のjqueryで正常に動作click ます

$(document).ready(function(){


        $('.call-parent').parent_child({
            target: '.element'
        });

        $('.call-child-1').click(function(){
            $(this).parent_child().child_1();
            return false;
        });

        $('.call-child-2').click(function(){
            $(this).parent_child().child_2();
            return false;
        });

    });

(function($) {

        $.fn.extend({ 

            parent_child: function(options) {

                var defaults = {
                    target:''
                }

                var options = $.extend(defaults, options);
                var o = options;

                var $this = this;

                var $cm = this.click(function(ei) {


                    alert('parent');
                    $this.child_1();

                    return false;

                });


                $this.child_1 = function() {

                    alert('child 1');

                };


                $this.child_2 = function() {

                    alert('child 2');

                };

                return $cm;

            }
        })
    })(jQuery);​

eachしかし、プラグインを使用したりready、プラグインを使用したりすると、エラーが発生します。

$(document).ready(function(){


        $('.call-parent').parent_child({
            target: '.element'
        });

        $('.call-child-1').click(function(){
            $(this).parent_child().child_1();
            return false;
        });

        $('.call-child-2').click(function(){
            $(this).parent_child().child_2();
            return false;
        });

    });

(function($) {

        $.fn.extend({ 

            parent_child: function(options) {

                var defaults = {
                    target:''
                }

                var options = $.extend(defaults, options);
                var o = options;

                var $this = this;

                var $cm = this.each(function(ei) {


                    alert('parent');
                    $this.child_1();

                    return false;

                });


                $this.child_1 = function() {

                    alert('child 1');

                };


                $this.child_2 = function() {

                    alert('child 2');

                };

                return $cm;

            }
        })
    })(jQuery);​

エラーメッセージ、

$this.child_1は関数ではありません[BreakOnThis Error]

なぜ私はまたはでそれを行うことができないのですeachready?それとも私はそれを間違ってしましたか?

4

2 に答える 2

0

あなたが受け入れることができるように、私は私のコメントを答えとして言い換えています:

のような関数式var f = function() { ... }は、名前付き関数宣言のように引き上げられませfunction f() { ... }ん。したがって、このeach例では、設定は宣言される前に$cm呼び出しています。$this.child_1最初の例では、関数はクリック時に呼び出されるため、実行されるまでにすでに解析されています。

于 2012-04-16T13:20:53.310 に答える
-1

$プラグインでは使用しないでください。これは、jQueryによる使用と競合します。aおそらく、他のものを使用してください。

于 2012-04-16T13:04:43.823 に答える