如何在 jQuery 中使用 removeClass() 方法?


若要使用 jQuery 在點選錨點標籤時為其新增一個類,請使用 addClass() 方法。若要刪除一個類,請使用 removeClass() 方法。

示例

你可以嘗試執行以下程式碼來學習如何使用 jQuery 去掉一個類 −

線上演示

<html>
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
          $("a").click(function(){
             $("a.active").removeClass("active");
             $(this).addClass("active");
           });
         });
      </script>
      <style>
         .active {
            font-size: 22px;  
         }
      </style>
   </head>
   <body>
      <a href="#" class="demo1">One</a>
      <a href="#" class="demo2">Two</a>
      <p>Click any of the link above and you can see the changes.</p>
   </body>
</html>

更新於:14-2 月-2020

95 次瀏覽

啟動你的 事業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.