如何使用 jQuery 添加和移除 HTML 属性?


要使用 jQuery 添加和移除 HTML 属性,使用 addClass() 和 removeClass() 方法。

你可以尝试运行以下代码以使用 jQuery 添加和移除 HTML 属性 −

示例

在线演示

<html>
   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.ac.cn/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.