要使用jQuery添加和删除HTML属性,请使用addClass()
andremoveClass()
方法。
您可以尝试运行以下代码以使用jQuery添加和删除HTML属性-
<html> <head> <title>jQuery Example</title> <script src = "https://cdn.staticfile.org/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>