【问题标题】:jquery plugin's chainability with each and readyjquery 插件的可链接性与每个就绪
【发布时间】:2012-04-27 19:26:57
【问题描述】:

我正在尝试在 jquery 插件中处理 chainability,它适用于 jquery click below

$(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);​

但是当我在插件中使用eachready 时出现错误,例如,

$(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 不是函数 [Break On This Error]

为什么我不能用eachready 来做呢?还是我做错了?

【问题讨论】:

  • 函数表达式不会被提升,所以当你设置$cm时,$this.child_1还不存在。
  • 知道了。感谢您的回复。

标签: jquery jquery-plugins each ready chainability


【解决方案1】:

不要在你的插件上使用$。它与 jQuery 对它的使用相冲突。使用其他东西,a 也许。

【讨论】:

  • $是插件中jQuery的本地副本;这里没有冲突。
【解决方案2】:

我重申我的评论作为答案,以便您接受:

var f = function() { ... } 这样的函数表达式不会像命名函数声明 function f() { ... } 那样被提升。所以在each 示例中,设置$cm 是在声明之前调用$this.child_1。在您的第一个示例中,该函数在单击时被调用,因此它在执行时已经被解析。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2011-07-20
    • 2013-01-06
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多