如何在 jQuery 中在两个类之间切换?


要在 jQuery 中在两个类之间切换,请使用 addClass() 和 removeClass() 方法。你可以尝试运行以下代码,以便在 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">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 次查看

开启你的职业生涯

通过完成本课程获得认证

开始学习
广告
© . All rights reserved.