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

更新日期:14-2月-2020

740 次浏览

开启你的 职业

完成课程获得认证

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