第5章 jQueryの効果 - 非表示と表示
jQuery(ジェイクエリー)は、ウェブブラウザ用のJavaScriptコードをより容易に記述できるようにするために設計されたJavaScriptライブラリである。
homepage
# **jQueryの効果 - 非表示と表示** *** ## **1.hide()およびshow()** jQueryでは、`hide()` and `show()` メソッドを使ってHTML要素を隠したり表示したりできます。 ### **構文:** ``` $(selector).hide(speed,callback); $(selector).show(speed,callback); ``` * オプションのspeedパラメータは、非表示/表示の速度を指定します。値は "slow"、 "fast"、またはミリ秒です。 * オプションのコールバックパラメータは`hide()`or `show()`メソッドが完了した後に実行される関数です(コールバック関数の詳細については後の章で学びます)。 <br> #### 例 [sample5-1.html] ``` <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>If you click on the "Hide" button, I will disappear.</p> <button id="hide">Hide</button> <button id="show">Show</button> </body> </html> ``` #### 実行結果 <iframe src="javascript: '<html><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script><body> <p>If you click on the "Hide" button, I will disappear.</p> <button id="hide" style="font-size:inherit">Hide</button> <button id="show" style="font-size:inherit;margin-left:10px">Show</button> </body></html>'" width="800"></iframe> <br><br> 次の例では、speedパラメータを示しています`hide()`。 #### 例 [sample5-2.html] ``` <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(1000); }); }); </script> </head> <body> <button>Hide</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body> </html> ``` #### 実行結果 <iframe src="javascript: '<html><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(1000); }); }); </script><body> <button style="font-size:inherit">Hide</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body></html>'" width="800"></iframe> ## **2.toggle()** `toggle()`メソッドを使用して要素を非表示にしたり表示したりすることもできます。 表示されている要素は非表示になっており、非表示の要素が表示されています。 ### **構文:** ``` $(selector).toggle(speed,callback); ``` * オプションのspeedパラメータには、 "slow"、 "fast"、またはミリ秒の値を指定できます。 * オプションのコールバックパラメータは、`toggle()`完了後に実行される関数 です。 #### 例 [sample5-3.html] ``` <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); </script> </head> <body> <button>Toggle between hiding and showing the paragraphs</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body> </html> ``` #### 実行結果 <iframe src="javascript: '<html><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); </script><body> <button style="font-size:inherit">Toggle between hiding and showing the paragraphs</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body></html>'" width="800"></iframe> *** > **練習** >> 問題[Ex5-1.html]
content
戻る