如何在 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>

更新日期:2020 年 2 月 14 日

95 次瀏覽

開啟您的職業

完成課程獲得認證

開始
廣告
© . All rights reserved.