如何使用 jQuery 在點選錨點標籤時新增一個類?


若要使用 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.