私はjQueryプラグインに取り組んでおり、すでに興味深い問題に直面しています。ここに私のコードサンプルがあります:
(function($){
var settings = {
'speed': 500
};
var methods = {
init: function(options){
settings = $.extend(options);
return this.each(function(){
$(this).bind('click.hideParagraph', methods.manage );
});
},
manage: function(){
if ($(this).hasClass("hidden")){
methods.show(this);
} else {
methods.hide(this);
}
},
show: function(item){
$(item).fadeIn(settings.speed, function(){});
},
hide: function(item){
$(item).fadeOut(settings.speed, function(){});
},
destroy: function(){
return this.each(function(){
$(this).unbind('click.hideParagraph');
});
}
};
$.fn.hideParagraph = 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.hideParagraph' );
}
};
})(jQuery);
主な問題は、fadeIn() 関数と fadeOut() 関数がこのように機能しないことです (エラー: f.speed は 'jquery.min.js' の関数ではありません)。なぜそれが起こるのか誰にも分かりますか?
もう 1 つの質問: これは jQuery プラグインを実装する正しい方法ですか? より便利で柔軟な方法を提案できる人はいますか?
ありがとう。