2010-11-05
■[JavaScript][jQuery][まとめ]jQueryエントリのまとめ
ここまでのjQuery関連のエントリのまとめです。
エントリ
- jQueryをはじめました - プログラマーな日々
- DOM要素の作成、操作、挿入 - プログラマーな日々
- jQueryで種類を指定してフォーム要素を選択する - プログラマーな日々
- グローバル名前空間での競合を回避する - プログラマーな日々
- インデックスを指定して要素を絞り込む - プログラマーな日々
- eachを使ってオブジェクトをループにかける - プログラマーな日々
- プロファイラを作成する - プログラマーな日々
- 要素のフェードイン・フェードアウト - プログラマーな日々
- 要素が見えるようにスクロールする - プログラマーな日々
- 複数のイベントにハンドラをバインドする - プログラマーな日々
- 必要なデータをイベントハンドラに提供させる - プログラマーな日々
- jQuery UIのオートコンプリート機能 - プログラマーな日々 10/05 19:00追加
- 1つのチェックボックスに基づいてすべてのチェックボックスを切り替える - プログラマーな日々
- ドロップダウンメニュー - プログラマーな日々
- jQuery UIのテーマ - プログラマーな日々
- jQueryでAjaxを実装する - プログラマーな日々
- QUnitでJavaScriptのユニットテストを自動化する - プログラマーな日々
参考にしたリソース
- jQueryクックブック
- jQuery: The Write Less, Do More, JavaScript Library
- jQuery UI - Home
- Google Libraries API - Developer's Guide - Make the Web Faster — Google Developers
- jQuery 日本語リファレンス
- jQuery基礎文法最速マスター[to-R]
- jQueryのテスティングフレームワークQUnit (でぃべろっぱーず・さいど)
- QUnit - jQuery JavaScript Library 11/05 18:00 QUnitのサイトを追加
■[JavaScript][jQuery][TDD]QUnitでJavaScriptのユニットテストを自動化する
QUnitを使うとJavaScriptのユニットテストを自動化できます。テストには以下が必要です。
- testsuite.cssのインクルード
- testrunner.jsのインクルード
- idが"main"のdiv
詳細はソースコードを見てください。
ソースコード
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>QUnit sample</title> <script src="http://code.jquery.com/jquery-latest.js"> </script> <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/qunit/testsuite.css" type="text/css" media="screen" /> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/qunit/testrunner.js"> </script> <script type="text/javascript"> $(function () { try { // ここにテストケースを書く test("a basic test example", function () { // 第1引数がtrueか? ok(true, "this test is fine"); var value = "hello"; // 第1引数と第2引数が一致しているか? equals("hello", value, "We expect value to be hello"); }); } catch (e) { } }); </script> </head> <body> <h1> QUnit example</h1> <h2 id="banner"> </h2> <h2 id="userAgent"> </h2> <ol id="tests"> </ol> <div id="main"> </div> </body> </html>
テスト結果
■[JavaScript][jQuery]jQueryでAjaxを実装する
ソースコード
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <div id="contents"></div> <input id="load" type="button" value="load" /> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script type="text/javascript"> (function ($) { $(document).ready(function () { $('#load').click(function () { // Ajaxリクエストのレスポンスを表示 $('#contents').load('HelloWorld.html'); }); }) })(jQuery); </script> </html>
ボタンクリック前
ボタンクリック後
■[JavaScript][jQuery]jQuery UIのテーマ
jQuery UIにはテーマと呼ばれるCSSのセットが用意されています。これを適用することで一貫したスタイルを実現できます。
テーマはこちらからダウンロードできます。jQuery UI - Configure your download
jQuery UIのオートコンプリート機能 - プログラマーな日々で作成したページにテーマを適用してみます。
普通にスタイルシートを読み込むだけです。
<link rel="stylesheet" type="text/css" href="Styles/jquery-ui-1.8.6.custom.css" />
適用前
適用後
■[JavaScript][jQuery]ドロップダウンメニュー
ソースコード
<!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="en-us" lang="en-us"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="imagetoolbar" content="false" /> </head> <body> <div id="container"> <ul class="dropdown" style="width: 200px; border: 1px solid #000000"> <li class="dropdown_trigger"><a href="#">ナビゲーションメニュー</a> <ul> <li><a href="#">メニュー1</a></li> <li><a href="#">メニュー2</a></li> <li><a href="#">メニュー3</a></li> <li><a href="#">メニュー4</a></li> <li><a href="#">メニュー5</a></li> </ul> </li> </ul> </div> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> </script> <script type="text/javascript"> (function ($) { function initDropdown() { if (!$('ul.dropdown').length) { return } // ナビゲーションメニューのhoverでサブメニューの表示/非表示を切り替える $('ul.dropdown li.dropdown_trigger').hover(function () { $(this).find('ul').fadeIn(1); }, function () { $(this).find('ul').hide(); }); } $(document).ready(function () { initDropdown(); }); })(jQuery); </script> </html>
動作
表示前
ナビゲーションメニューをマウスオーバー
■[JavaScript][jQuery]jQuery UIのオートコンプリート機能
ソースコード
「jquery-ui.js」をインクルードする必要があります。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <label for="month">月選択</label> <input id="month" type="text" name="month" /> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.js"></script> <script type="text/javascript"> (function ($) { $(document).ready(function () { var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; $("#month").autocomplete({ source: months }); }); })(jQuery); </script> </html>
動作
■[JavaScript][jQuery]1つのチェックボックスに基づいてすべてのチェックボックスを切り替える
ソースコード
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <fieldset> <legend>Reasons to be happy</legend><a class="selectAll" href="#">Select All</a> <a class="deselectAll" href="#">Deselect All</a> <input name="reasons" id="iwokeup" type="checkbox" value="iwokeup" /> <label for="iwokeup">I woke up</label> <input name="reasons" id="health" type="checkbox" value="health" /> <label for="health">My health</label> <input name="reasons" id="family" type="checkbox" value="family" /> <label for="family">My family</label> <input name="reasons" id="sunshine" type="checkbox" value="sunshine" /> <label for="sunshine">The sun is shining</label> </fieldset> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> <script type="text/javascript"> (function ($) { $(document).ready(function () { $('fieldset .selectAll').click(function (event) { // アンカー本来の動作を中止する event.preventDefault(); // 兄弟要素のチェックボックスを全てチェックする $(this).siblings('input:checkbox').attr('checked', 'checked'); }); $('fieldset .deselectAll').click(function (event) { event.preventDefault(); $(this).siblings('input:checkbox').removeAttr('checked'); }); }); })(jQuery); </script> </html>
すべてチェック
アンカークリック前
アンカークリック後
すべてチェック解除
アンカークリック前
アンカークリック後
■[JavaScript][jQuery]必要なデータをイベントハンドラに提供させる
ソースコード
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <input id="first" type="button" value="first" /> <input id="last" type="button" value="last" /> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> <script type="text/javascript"> (function ($) { $(document).ready(function () { $('#first').bind('update', function (e) { e.firstName = this.value; }); $('#last').bind('update', function (e) { e.lastName = this.value; }); var e = $.Event('update'); // Eventオブジェクトはハンドラの第1引数と同じ $('#first, #last').trigger(e); // イベントの実行 alert(e.firstName); alert(e.lastName); }); })(jQuery); </script> </html>

















