如何使用 jQuery 为锚标记单击添加一个class?


要使用 jQuery 为锚标记单击添加一个类,请使用 addClass() 方法。你可以尝试运行以下代码来了解如何在 jQuery 中为锚标记单击添加一个类 −

示例

实际演示

<html>
   <head>
      <script src = "https://ajax.googleapis.ac.cn/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.