【问题标题】:Calling other plugins within a jQuery plugin在 jQuery 插件中调用其他插件
【发布时间】:2010-11-17 08:28:17
【问题描述】:

为了清理我的代码,我想在我的实际 jQuery 插件中使用子插件,但实际上什么也没发生。提前谢谢

作为一个简单的例子,请看下面的代码:

(function($){
    $.fn.funct = function() {
        // so far it seems to run the code...
        console.log('funct is running...');

        return this.each(function(){
            // ...but nothing is happening here
            console.log('this.each is running...');
            $(this).css('background', 'blue');
        }
    } 
    $.fn.foo = function() { 
        return this.each(function(){
            console.log('plugin is running...');
            $(this).funct();
        });
    };
})(jQuery);

【问题讨论】:

    标签: javascript jquery ajax jquery-plugins


    【解决方案1】:

    乍一看,您似乎没有正确关闭第一个退货。

    $(this).css('background', 'blue');
            }
    

    应该是:

    $(this).css('background', 'blue');
            });
    

    【讨论】:

      【解决方案2】:

      我希望在一个插件中触发一个自定义事件,并让另一个插件订阅该事件。你没有依赖。

      有关自定义事件和绑定/触发的更多信息,请参阅我的回答 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多