Last active
May 26, 2017 08:55
-
-
Save ethertank/2270538 to your computer and use it in GitHub Desktop.
オプション付きjQueryプラグインの最小構成テンプレート
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 先頭行: 他のスクリプトでのセミコロン忘れに備え、且つ、予約語でないundefinedの書換えによる誤動作を防いでいる。*/ | |
;(function($, undefined) { | |
"use strict"; | |
$.fn.myPlugIn = function(option) { | |
var a, b, c; | |
//※未設定のオプション項目に初期値を設定 | |
option = $.extend({ | |
opt1: null, | |
opt2: null | |
}, option); | |
//※プラグイン内部でのみ使用される関数 | |
function DoSomething(e) { | |
} | |
//※対象要素群のそれぞれに対し何かする | |
this.each(function() { | |
DoSomething($(this)); | |
}); | |
//※外部から参照可能な関数 | |
$.fn.publicMethod = function( options ) { | |
// | |
}; | |
// 何かする | |
return this; //※メソッドチェーン対応の為に return しておく | |
}; | |
})(jQuery); | |
/* 最終行: 第二引数は指定しない。undefined が 先頭行の undefined 引数に設定される */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// オプションを省略した実行コード | |
jQuery(function($) { | |
$("#selector").myPlugIn(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// オプション付きの実行コード | |
jQuery(function($) { | |
$("#selector").myPlugIn({ | |
opt1 : 100, | |
opt2 : "Love is Danger" | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment