源代碼
點擊運行
<!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").addClass("blue"); $("div").addClass("important"); }); }); </script> <style type="text/css"> .important { font-weight:bold; font-size:xx-large; } .blue { color:blue; } </style> </head> <body> <h1>標題 1</h1> <h2>標題 2</h2> <p>這是一個段落。</p> <p>這是另外一個段落。</p> <div>這是一些重要的文本!</div> <br> <button>為元素添加 class</button> </body> </html>
運行結果