第16章 jQuery - css()メソッド
jQuery(ジェイクエリー)は、ウェブブラウザ用のJavaScriptコードをより容易に記述できるようにするために設計されたJavaScriptライブラリである。
homepage
# **jQuery - css()メソッド** *** `css()`メソッドは、選択した要素に対して1つ以上のスタイルプロパティを設定または返します。 ## **1.CSSプロパティを返す** 指定要素のCSSプロパティを返すには、次の構文を使用します。 ``` css("propertyname"); ``` #### **例** [sample16-1.html] ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert("background-color = " + $("p").css("background-color")); }); }); </script> </head> <body> <h2>This is a heading</h2> <p style="background-color:#ff0000">This is a paragraph.</p> <p style="background-color:#00ff00">This is a paragraph.</p> <p style="background-color:#0000ff">This is a paragraph.</p> <button>Get background-color of first p</button> </body> </html> ``` #### **実行結果** <pre><span class="nocode"> <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(){ alert("background-color = " + $("p").css("background-color")); }); }); </script> </head> <body> <h2>This is a heading</h2> <p style="background-color:#ff0000">This is a paragraph.</p> <p style="background-color:#00ff00">This is a paragraph.</p> <p style="background-color:#0000ff">This is a paragraph.</p> <button style="font-size:inherit">Get background-color of first p</button> </body> </html>'" width="600" height="250"></iframe> </span> </pre> <br> ## **2.CSSプロパティを設定** 複数のCSSプロパティを設定するには、次の構文を使用します。 ``` css({"propertyname":"value","propertyname":"value",...}); ``` 次の例では、一致したすべての要素に対して背景色とフォントサイズを設定します。 #### **例** [sample16-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").css({"background-color": "yellow", "font-size": "200%"}); }); }); </script> </head> <body> <h2>This is a heading</h2> <p style="background-color:#ff0000">This is a paragraph.</p> <p style="background-color:#00ff00">This is a paragraph.</p> <p style="background-color:#0000ff">This is a paragraph.</p> <p>This is a paragraph.</p> <button>Set multiple styles for p</button> </body> </html> ``` #### **実行結果** <!DOCTYPE html> <pre><span class="nocode"> <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").css({"background-color": "yellow", "font-size": "200%"}); }); }); </script> </head> <body> <h2>This is a heading</h2> <p style="background-color:#ff0000">This is a paragraph.</p> <p style="background-color:#00ff00">This is a paragraph.</p> <p style="background-color:#0000ff">This is a paragraph.</p> <p>This is a paragraph.</p> <button style="font-size:inherit">Set multiple styles for p</button> </body> </html>'" width="600" height="300"></iframe> </span> </pre> *** > **練習** >> **問題**[Ex16-1.html] [sample16-2.html]をもとにして、すべてのP要素の背景色("Lightpink")とフォントサイズ("80%")を設定しましょう。
content
戻る