Add/delete class names in jquery CSS

Source: Internet
Author: User

Addclass (class)-Add the specified class name for each matching element.
Parameter: Class-names of one or more CSS classes to be added to an element. Separate them with spaces (string)
Example 1:
Add the 'selected' class to the matched element.
HTML code:
<P> Hello </P>
Jquery code:
$ ("P"). addclass ("selected ");
Result:
[<P class = "selected"> Hello </P>]
Example 2:
Add the selected highlight class to the matched element
HTML code:
<P> Hello </P>
Jquery code:
$ ("P"). addclass ("selected highlight ");
Result:
[<P class = "selected highlight"> Hello </P>]

Addclass (function (index, class)-adds the specified class name for each matching element.

Parameter: function (index, class)-This function must return one or more class names separated by spaces. Two parameters are accepted. The index parameter is the index value of the object in this set, and the class parameter is the original class attribute value of this object. (Function)
Example: add different classes to Li
HTML code:
<Ul> <li> Hello </LI> </ul>
Jquery code:
$ ('Ul Li: la'). addclass (function (){
Return 'item-'+ $ (this). Index ();
});

Removeclass ([Class])-deletes all or specified classes from all matching elements.

Parameter: Class (optional)-one or more CSS class names to be deleted. Separate them with spaces (string)
Example:
Delete the 'selected' class from the matched element.
HTML code:
<P class = "selected first"> Hello </P>
Jquery code:
$ ("P"). removeclass ("selected ");
Result:
[<P class = "first"> Hello </P>]

Delete all classes of matching elements
HTML code:
<P class = "selected first"> Hello </P>
Jquery code:
$ ("P"). removeclass ();
Result:
[<P> Hello </P>]

Removeclass (function (index, class)-deletes all or specified classes from all matching elements.
Parameter: function (index, class)-This function must return one or more class names separated by spaces. Two parameters are accepted. The index parameter is the index value of the object in this set, and the class parameter is the original class attribute value of this object. (Function)
Example:
Delete the class on the last element that is repeated with the previous one.
Jquery code:
$ ('Li: la'). removeclass (function (){
Return $ (this). Prev (). ATTR ('class ');
});

Toggleclass (class)-if there is (does not exist), delete (ADD) a class.
Parameter: Class-CSS Class Name (string)
Example:
Switch the 'selected' class to the matching element.
HTML code:
<P> Hello </P> <P class = "selected"> hello again </P>
Jquery code:
$ ("P"). toggleclass ("selected ");
Result:
[<P class = "selected"> Hello </P>, <p> hello again </P>]

Toggleclass (class, switch)-if the switch parameter is true, add the corresponding class; otherwise, delete the switch.
Parameters:
Class-name of the CSS class to be switched (string)
Switch-Boolean is used to determine whether an element contains a class)
Example:
Add the 'highlight' class every three clicks.
HTML code:
<Strong> jquery code: </strong>
Jquery code:
VaR COUNT = 0;
$ ("P"). Click (function (){
$ (This). toggleclass ("highlight", Count ++ % 3 = 0 );
});

Toggleclass (function (index, class), [Switch])-if the switch parameter is true, the corresponding class is added; otherwise, the class is deleted.
Parameters:
Function (index, class)-returns a function with the class name. It accepts two parameters. index indicates the index position of the element in the Set, and class indicates the class value of the original element (function)
Switch-(optional) is used to determine whether an element contains a Boolean value of class)
Example:
Set the Class Attribute Based on the parent element.
Jquery code:
$ ('Div. foo'). toggleclass (function (){
If ($ (this). Parent (). Is ('. bar '){
Return 'Happy ';
} Else {
Return 'sad ';
}
});

Category: jquery
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.