なぜか質問を投稿すると反映されない。。 何かしら問題があってはじかれている??
jQueryのプラグインで複数のメソッドを使用する
プラグイン
(function($){
var methods = {
init : function(){
console.log('initial');
},
foo : function(){
console.log('foo');
},
bar : function(options){
console.log(options);
}
};
$.fn.hogehoge = 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 console.log(method);
};
})(jQuery);
呼び出し
$(selecter).hogehoge();
// initial
$(selecter).hogehoge('foo');
// foo
var opts = { 'spam' : 'ham' };
$(selecter).hogehoge('bar', opts);
// { 'spam' : 'man' }
検索

コメントを残す