源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").toggleClass("blue"); }); }); </script> <style type="text/css"> .blue { color:blue; } </style> </head> <body> <h1 class="blue">標題 1</h1> <h2 class="blue">標題 2</h2> <p class="blue">這是一個段落。</p> <p>這是另外一個段落。</p> <br> <button>切換 class</button> </body> </html>
運行結果