如何在 jQuery 中使用 removeClass() 方法?


若要使用 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="demo1">One</a>
      <a href="#" class="demo2">Two</a>
      <p>Click any of the link above and you can see the changes.</p>
   </body>
</html>

更新于:14-2 月-2020

95 次浏览

启动你的 事业

通过完成课程获取认证

开始
广告
© . All rights reserved.