Background: Add and delete classes or attributes to HTML elements dynamically through jquery, so that HTML elements present different styles at different times, giving the user a better sense of experience.
If the following P fragment and button buttons are present, the code is as follows:
1 <p id="pdisplay"> now the background color is white, click the button after the background changed to red </p>2 <button id= " ChangeColor "> Change Colors </button>
The following CSS code exists:
1 <style>2 . colorred{3 background-color:red; 4 }5 </style>
The following JS code exists:
$ ("#changeColor"). Toggle (function () { $ ("#pDisplay "). AddClass ("colorred"); }, function () { $ (" #pDisplay "). Removeclass ("colorred"); });
As the code above, when the button is clicked, add or remove the CSS class colorred to the $ ("#pDisplay") element as follows:
Similarly, it can be applied to attr () and removeattr () with the following code:
1 //Disabled settings element is not available:2 3$( This). attr ("Disabled","Disabled")4 5 //Remove the diasble attribute from the push element:6 7$("#push"). Removeattr ('Disabled')
jquery dynamically additions or deletions to an element class or attribute