ก่อนอื่นต้องรู้จักการใช้งาน หรือสามารถเขียน CSS พื้นฐาน หรืออาจมีความเข้าใจเกี่ยวกับ CSS บ้างเล็กน้อยเพื่อให้สามารถใช้งาน CSS Classes ได้อย่างถูกต้อง
ตัวอย่างการเขียน CSS (พื้นฐาน)
<style> .txt{ color: red; font-size : 16px; } p{ color : blue; font-weight : bold; } </style>
จากตัวอย่างด้านบน เป็นการกำหนดรูปแบบหรือคุณลักษณะให้กับคลาส txt และแท็ก <p>
ซึ่งการจัดการ CSS ของ jquery สามารถทำได้โดยใช้เมธอด
- addClass() เพิ่มคลาส 1 หรือมากกว่า ให้กับ element ที่เลือก
- removeClass() ลบคลาส 1 หรือมากกว่า ให้กับ element ที่เลือก
- toggleClass() สลับระหว่าง เพิ่ม/ลบ คลาสให้กับ element ที่เลือก
- css() กำหนดคุณลักษณะของเอกสาร
1.addClass()
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").addClass("important blue"); }); }); </script> <style> .important { font-weight: bold; font-size: xx-large; } .blue { color: blue; } </style> </head> <body> <div id="div1">This is some text.</div> <div id="div2">This is some text.</div> <br> <button>Add classes to first div element</button> </body> </html>
ไฟล์ที่ 1 addClass()
2.removeClass()
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1, h2, p").removeClass("blue"); }); }); </script> <style> .blue { color: blue; } </style> </head> <body> <h1 class="blue">Heading 1</h1> <h2 class="blue">Heading 2</h2> <p class="blue">This is a paragraph.</p> <p>This is another paragraph.</p> <button>Remove class from elements</button> </body> </html>
ไฟล์ที่ 2 removeClass()
3.toggleClass()
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1, h2, p").toggleClass("blue"); }); }); </script> <style> .blue { color: blue; } </style> </head> <body> <h1>Heading 1</h1> <h2>Heading 2</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Toggle class</button> </body> </html>
ไฟล์ที่ 3 toggleClass()
4.css()
ไวยากรณ์ในการกำหนด css ของ jQuery
แบบที่ 1 คุณลักษณะเดียว
css("ชื่อคุณลักษณะ", "ค่า");
แบบที่ 2 หลายคุณลักษณะ
css({"ชื่อคุณลักษณะ":"ค่า","ชื่อคุณลักษณะ":"ค่า", ...});
ตัออย่างการใช้งาน CSS() แบบที่ 1
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").css("background-color", "yellow"); }); }); </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 background-color of p</button> </body> </html>
ไฟล์ที่ 4 css()-1
ตัวอย่างการใช้งาน CSS() แบบที่ 2
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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>
ไฟล์ที่ 5 css()-2