如何使用 jQuery 為錨標記單擊新增一個class?


要使用 jQuery 為錨標記單擊新增一個類,請使用 addClass() 方法。你可以嘗試執行以下程式碼來了解如何在 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</a>
      <a href="#" class="">Two</a>
      <p>Click any of the link above and you can see the changes.</p>
   </body>
</html>

更新時間:2020 年 2 月 13 日

瀏覽量 1 千+

開啟你的 事業

完成課程獲得認證

開始吧
廣告
© . All rights reserved.