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

更新於: 2020 年 2 月 14 日

740 次檢視

開啟你的職業生涯

透過完成本課程獲得認證

開始學習
廣告