JQuery adds/changes/removes CSS classes, jquerycss

Source: Internet
Author: User

JQuery adds/changes/removes CSS classes, jquerycss

You can also use Javascript to change the page element style. But is there any more concise method? The answer is yes. Now with jQuery, it seems that Javascript code has been reduced a lot, the following sentence was fulfilled: "jQuery makes JavaScript code concise! ", Let's get down to the truth and see how jquery adds and removes CSS classes:

1. removeClass ()-remove the CSS class

. Code
  1. $ ("# Target"). removeClass ("oldClass ");
  2. // # Target indicates the ID of the element to be removed from the CSS class.
  3. // OldClass refers to the CSS Class Name

 

2. addClass ()-add CSS class. Front-end UI framework sharing

. Code
  1. $ ("# Target"). addClass ("newClass ");
  2. // # Target indicates the ID of the element to add a style.
  3. // NewClass 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.

. Code
  1. $ ("# Target"). toggleClass ("newClass ")
  2. // If the element whose ID is "target" has defined the CSS style, it will be removed;
  3. // Conversely, the CSS class "newClass" will be assigned to this ID.

 

4. hasClass ("className")-determine whether the CSS front-end UI framework already exists

In practice, we usually define these CSS classes first, and then use Javascript events to trigger (such as clicking a button) to change the page element style. In addition, jQuery provides a hasClass ("className") method to determine whether an element has been assigned to a CSS class. By the way, I would like to tell the beginner in front-end development that jquery is worth it. If you have time, study it.


How can I change the css style with jquery?

You can use the css and addClass mentioned above. The css method adds a style to the style attribute of the tag, while addclass adds a class to the tag, you only need to add the style of this class. As the name suggests, removeClass deletes a class, because jquery can add multiple classes to a label. In addition, toggleClass ("aa") means to delete a tag if it has Class aa. If it does not exist, add

Jquery removed CSS styles

Upstairs, the main author is to jquery...

$ (Document). ready (function (){
$ ('# Msg5. syzx span'). removeAttr ("style ");
});

It's actually removeAttr ("style"). The structure of your code depends on how you get the span tag.

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.