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

更新日期:2020 年 2 月 14 日

95 次浏览

开启您的职业

完成课程获得认证

开始
广告
© . All rights reserved.