如何使用 jQuery 刪除所有 CSS 類?


若要刪除所有類,請直接使用 removeClass() 方法,無引數。這樣便會刪除元素的所有類。

示例

你可以嘗試執行下列程式碼,使用 jQuery 刪除所有 CSS 類

線上演示

<html>
   <head>
      <title>jQuery Selector</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   
      <script>
         $(document).ready(function() {
           $("#button1").click(function(){
             $("p").removeClass();
           });
         });
      </script>
       
      <style>
         .red { color:red; }
         .blue { color:blue; }
      </style>
   </head>
   
   <body>
      <p class = "red" >This is first paragraph.</p>
      <p class = "blue">This is second paragraph.</p>
      <button id="button1">Remove</button>
   </body>
</html>

更新於: 06-Dec-2019

5K+ 瀏覽

開啟你的 職業

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.