jQueryプラグイン

2011年8月15日 Posted by PURGE

/*
 * jQueryテストプラグイン
 */
;(function($){
	$.fn.myTest = function(options){
		var opts = jQuery.extend({}, $.fn.myTest.defaults, options);

		return this.each(function(){
			$(this).css({width : opts.width, 
						  height : opts.height,
						  background : opts.color});
		});
	};
	//デフォルト値
	$.fn.myTest.defaults = {
		height : '200px',
		width :  '300px',
		color : 'blue'
	};

})(jQuery);

もし、引数にてパラメータを渡した場合デフォルト値が上書きされる。

$(function(){
	$("#test").myTest({width : '400px'});
})
<div id="test"></div>

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です