如何使用 jQuery 新增和移除 HTML 屬性?


要使用 jQuery 新增和移除 HTML 屬性,使用 addClass() 和 removeClass() 方法。

你可以嘗試執行以下程式碼以使用 jQuery 新增和移除 HTML 屬性 −

示例

線上演示

<html>
   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type = "text/javascript">
         $(document).ready(function(){
         
           $( '#add' ).click( function () {
               $('#page_navigation1').addClass( 'blue-class' );        
           });
         
           $( '#remove' ).click( function () {        
               $('#page_navigation1').removeClass( 'blue-class' );        
           });    
         });  
         
      </script>
      <style>
         .blue-class {
           background-color: blue;
           font-size: 30px;
         }
      </style>
   </head>
   <div id="page_navigation1">Demo</div>
   <button id="add">Add</button>
   <button id="remove">Remove</button>
</html>

更新於: 14-2-2020

614 次瀏覽

開啟你的 職業

完成課程認證

開始學習
廣告
© . All rights reserved.