JQueryプラグイン雛形

更新日 2025-06-09 14:16:05
js

JQueryプラグイン雛形


(function( $ ){

var methods = {
init : function( options ) {

// ここでは"this"はすでにjQueryオブジェクトなので
// $(this)を行う必要はありません

// 処理
// 渡された任意のオプションで拡張されるデフォルトをいくつか作成する
var settings = $.extend( {
'location' : 'top',
'background-color' : 'blue'
}, options);

},
show : function( ) {
// 処理
},
hide : function( ) {
// 処理
},
update : function( content ) {
// 処理
}
};

$.fn.tooltip = function( method ) {

// ここでは"this"はすでにjQueryオブジェクトなので
// $(this)を行う必要はありません

// メソッド呼び出し部分
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 );

// 初期化メソッドの呼び出し
$('div').tooltip();

// 初期化メソッドの呼び出し
$('div').tooltip({
foo : 'bar'
});