【发布时间】:2011-02-04 16:57:00
【问题描述】:
所以我正在尝试学习如何基于此示例为插件实现方法集合:http://docs.jquery.com/Plugins/Authoring
我无法理解的是如何将使用插件默认值扩展的选项发送到各个方法。
我很确定任何原始选项都会发送到这里的方法:
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
那么如何使用默认值扩展这些参数?该示例并未真正定义如何执行此操作...
var methods = {
init : function( options ) {
return this.each(function(){
$(window).bind('resize.tooltip', methods.reposition);
});
}
}
另外,这里是来自示例插件创作页面的代码:
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function(){
$(window).bind('resize.tooltip', methods.reposition);
});
},
destroy : function( ) {
return this.each(function(){
$(window).unbind('.tooltip');
})
},
reposition : function( ) { // ... },
show : function( ) { // ... },
hide : function( ) { // ... },
update : function( content ) { // ...}
};
$.fn.tooltip = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery );
【问题讨论】:
标签: javascript jquery plugins