jQuery APIリファレンスjQuery Core:プラグイン
- jQuery.fn.extend()〔jQuery要素に新しいメソッドを追加し、jQuery要素を拡張 〕
- jQuery.extend()〔Jqueryオブジェクト自体を拡張〕
jQuery.fn.extend()
jQuery要素に新しいメソッドを追加し、jQuery要素を拡張
2009/2/27
jQuery.fn.extend(object) 戻り値:jQuery
jQuery要素に新しいメソッドを追加し、jQuery要素を拡張します。
この関数は、jQueryプラグインを作るのに使用します。
-
第1引数objectには、jQueryオブジェクトに結合するオブジェクトを指定します。

$.fn.extend()の使用例サンプルを見る
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>jQuery Core:プラグイン:$.fn.extend()の使用例 | jQuery</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ /* 2つのプラグインメソッドを追加 */ $.fn.extend({ check: function() { /* 全要素にチェックを入れる */ return this.each(function() { this.checked = true; }); }, uncheck: function() { /* 全要素のチェックを外す */ return this.each(function() { this.checked = false; }); } }); /* tyep属性が「checkbox」のinput要素にチェックを入れる */ $("input[@type=checkbox]").check(); /* type属性が「radio」のinput要素のチェックを外す */ $("input[@type=radio]").uncheck(); }); </script> <style type="text/css"> #sample { color:blue; } #sample strong { color:red; } </style> </head> <body> <div id="wrap"> <h1>jQuery Core:プラグイン:$.fn.extend()の使用例 | jQuery</h1> <p>▼jQuery要素に「全要素にチェック入れる」プラグインと「全要素のチェックを外す」プラグインメソッドを追加する。</p> <!-- CODE --> <form action="#"> <p> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> </p> <p> <input type="radio" /> <input type="radio" /> <input type="radio" /> </p> </div> <!-- CODE / --> </div> </body> </html>
jQuery.extend()
Jqueryオブジェクト自体を拡張
2009/2/27
jQuery.extend(object) 戻り値:jQuery
jQueryオブジェクト自体を拡張します。
jQuery名前空間に関数を追加するのに使用します。
プラグインの追加方法は、$.fn.extendを参照してください。
-
第1引数objectには、jQueryオブジェクトに結合するオブジェクトを指定します。

$.extend()の使用例サンプルを見る
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>jQuery Core:プラグイン:$.extend()の使用例 | jQuery</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $.extend({ /* 2つの値を比較して、最小値を返す */ min: function(a, b) { return a < b ? a : b; }, /* 2つの値を比較して、最大値を返す */ max: function(a, b) { return a > b ? a : b; } }); $("#res strong:first").text($.min(2,3)); $("#res strong:last").text($.max(4,5)); }); </script> <style type="text/css"> #res strong { color:red; } </style> </head> <body> <div id="wrap"> <h1>jQuery Core:プラグイン:$.extend()の使用例 | jQuery</h1> <p>▼jQuery名前空間に「2つの値を比較して最大値を返す」関数と「2つの値を比較して最小値を返す」関数を追加します。</p> <!-- CODE --> <p id="res"><code>$.min(2,3)</code>の結果は、<strong></strong>です。<br><code>$.max(4,5)</code>の結果は、<strong></strong>です。</p> <!-- CODE / --> </div> </body> </html>