[jQuery] プラグイン 作り (1)
下の 具現を 参考すれば コンパニオン なる ことだ.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//プラグイン 具現部
$.fn.alertThisText = function(){
alert(this.text());
}
//プラグイン テスト
$('.alertThisText').click(function(){
$('.test').alertThisText();
});
});
</script>
<style>
.test{ width: 200px; height: 200px; text-align: center; padding-top: 50px; background: #eee;}
</style>
<div class="test">
abcdefg
</div>
<input type="button" class="alertThisText" value="test"/>
コードは 簡単だ. $.fn.プラグイン関数名前 = function(){} 形式だ. パラメーターを 受けるように 割 つもりならば 下のように 使う.
$.fn.プラグイン関数名前 = function(パラメーター){
//具現部. this増えた 関数を 呼び出した jQuery客体が なる.
}
さて 易しい.
- コメント機能はありません。コメントの代わりに[email protected]にメールを送ってください。