如何在 jQuery 中切換兩個類?


要在 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="one">One</a>
      <a href="#" class="two">Two</a>
      <p>Click any of the link above and you can see the changes.</p>
   </body>
</html>

更新日期:14-2月-2020

740 次瀏覽

開啟你的 職業

完成課程獲得認證

開始學習
廣告
© . All rights reserved.