jquery Add/Change/Remove CSS Classes

Source: Internet
Author: User

Transferred from: http://www.jbxue.com/article/24589.html

In jquery, use Removeclass to remove CSS classes, addclass add CSS classes, toggleclass Add or remove CSS classes, and hasclass determine if a CSS already exists.
Changing the page element style using JavaScript can also be achieved, but there is no more concise approach, the answer is yes, now with jquery, seems to make the JS code a lot of thin, fulfilled the sentence: "jquery makes JavaScript code concise!" "To get to the point, let's see how jquery adds and removes CSS classes:

1. Removeclass ()-Removing CSS classes
$ ("#target"). Removeclass ("Oldclass");
#target refers to the ID of the element that needs to be removed from the CSS class
Oldclass refers to the name of a CSS class

2.addClass ()-Add CSS Class
$ ("#target"). AddClass ("Newclass");
#target refers to the ID of the element for which you want to add a style
Newclass refers to the name of a CSS class

3. Toggleclass ()-Add or remove a CSS class: if the CSS class already exists, it will be removed and, conversely, if the CSS class does not exist, it will be added.
$ ("#target"). Toggleclass ("Newclass") www.jbxue.com
If the element with ID "target" already has a CSS style defined, it will be removed;
Conversely, the CSS class "Newclass" will be assigned to the ID

4.hasClass ("ClassName")-determine if CSS already exists
In practice, we usually define these CSS classes first, and then change the page element style by triggering JavaScript events, such as clicking on a button. In addition, jquery provides a method Hasclass ("ClassName") to determine whether an element has been assigned to a CSS class. By the way, to the front-end development of the novice, jquery is worth owning, have time to study it well.

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.