JQuary append deletion style and jquary append Style
<div></div>
Click Add style in jquery, and then click Remove style.
Hello !!
This problem can be solved through the toggle () function or toggleClass () function:
Toggle function, because it is based on click events to switch events, so here you only need to traverse li:
$ ("Ul> li "). each (function () {$ (this ). toggle (function () {detail (this).css ("backgroundColor", "# ccc") ;}, function () {detail (this).css ("backgroundColor ", "#000 ");});});
To use the toggleClass function, you must bind the click event to li:
$ ("Ul> li "). click (function () {$ (this ). toggleClass (function () {// rgb (204,204,204) ==# ccc if (callback (this).css ("backgroundColor") = "rgb (204,204,204)") {callback (this).css ("backgroundColor ", "#000");} else {detail (this).css ("backgroundColor", "# ccc ");}});});
In addition, toggle has been deleted in jQuery2.0,
Therefore, if you use the toggle function, pay attention to the jQuery version!
Of course, if you write # ccc and #000 in the form of @ Xiao Qi stew, the operation is more convenient!
Hope to help you !!
How does jquery dynamically add or delete class styles?
For example, if you use the attr () method to obtain the class of the p element, the JQuery code is as follows: var p_class = $ ("p "). attr ("class"); // obtain the class of the p element. Use the attr () method to set the class of the p element. The JQuery code is as follows: 1 $ ("p "). attr ("'class", "high"); // set the class of the p element to "high". In most cases, it replaces the original class with the new class, instead of appending a new class based on the original one. What is an append class? If the original class of the p element is myClass, after a class named high is appended, the class attribute becomes "myClass high", that is, the superposition of the myClass and high styles. JQuery provides a special addClass () method to append styles. To make the example easier to understand, add another style group in the style label: 1. high {color: red;} 2. another {font-style: italic; color: blue;} Then add a "APPEND class" button to the webpage. The event code of the button is as follows: 1 $ ("# btn_3 "). click (function () {2 $ ("# nm_p "). addClass ("another"); // append Style 3}); finally, when you click the "APPEND class" button, the p element style will become italic, the previous red font also turns blue. In this case, the p element has two class values, namely, "high" and "another ". There are two rules in CSS. 1. if multiple class values are added to an element, it is equivalent to merging their styles. 2. If different classes have the same style attribute set, the latter overwrites the former. In the preceding example, the following style is added to the p element: 1 color: red;/* set the font color to red */2 font-style: italic; 3 color: blue; there are two "color" attributes in the above style, and the "color" attribute will overwrite the previous "color" attribute, therefore, the final value of the "color" attribute is "blue" instead of "red ". Remove style if you want to delete a value of class when you click a button, you can use the removeClass () method opposite to addClass, it is used to delete all or specified classes from matching elements. For example, you can use the following JQuery code to delete a class with a value of "high" in the p element: 1 $ ("p "). removeClass ("high"); // remove the class whose value is "high" in the p element. If you want to delete both classes of the p element, you must use the removeClass () method twice, the Code is as follows: 1 $ ("p "). removeClass ("high "). removeClass ("another"); JQuery provides a simpler method. You can delete multiple class names by space. The Code is as follows: 1 $ ("p"). removeClass ("high another & quo ...... the remaining full text>