【发布时间】:2012-12-17 04:44:42
【问题描述】:
我一直在使用 jQuery Boilerplate 开发插件,但我不知道如何从插件外部调用方法。
作为参考,这里是我正在谈论的样板代码: http://jqueryboilerplate.com/
在我的小提琴中,
代码如下:
;(function ( $, window, document, undefined ) {
var pluginName = 'test';
var defaults;
function Plugin(element, options) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
this.hello();
},
hello : function() {
document.write('hello');
},
goodbye : function() {
document.write('goodbye');
}
}
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
}
})( jQuery, window, document );
$(document).ready(function() {
$("#foo").test();
$("#foo").test('goodbye');
});
我正在尝试使用以下语法调用再见方法:
$("#foo").test('goodbye')
我如何实现这一目标?提前致谢
【问题讨论】:
-
你可以看到my answer on this question。它有一个带有可访问方法的 jQuery 插件模板,实际上与这个非常相似,但不一样
-
来自 jquery 样板 wiki:github.com/jquery-boilerplate/jquery-boilerplate/wiki/…
标签: jquery boilerplate