Jquery basics-Add/Remove CSS classes

Source: Internet
Author: User

In webpage design, JavaScript is often used to change the style of page elements. One way is to change the CSS class of page elements. In traditional JavaScript, we usually process HTML domClassnameJquery provides three methods to implement this function. Although they are similar to traditional methods, they save a lot of time.Code. -"Jquery makes JavaScript code concise !"

1. addclass ()-add CSS class

 
$ ("# Target"). Addclass ("Newclass");
 
// # Target indicates the ID of the element to add a style.
// Newclass refers to the CSS Class Name

2. removeclass ()-remove the CSS class

 
$ ("# Target"). Removeclass ("Oldclass");
 
// # Target indicates the ID of the element to be removed from the CSS class.
// Oldclass refers to the CSS Class Name

3. toggleclass ()-add or remove a CSS class: If the CSS class already exists, it will be removed. On the contrary, if the CSS class does not exist, it will be added.

 
$ ("# Target"). Toggleclass ("Newclass")
 
// If the element whose ID is "target" has defined the CSS style, it will be removed;
// Conversely, the CSS class "newclass" will be assigned to this ID.

 

4.Hasclass ("classname")-determine whether CSS already exists

In practical use, we often define these CSS classes first, and then use JavaScript event triggers (such as clicking a link) to change the page element style. In addition, jquery provides a methodHasclass ("classname ")To determine whether an element has been assigned to a CSS class.

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.