CSS styles for JavaScript manipulation elements

Source: Internet
Author: User

we often use JavaScript to change the style of page elements. One way is to change the CSS class (class) of the page element, which in traditional JavaScript is typically done by handling the classname feature of the HTML DOM, while jquery provides three ways to do this, Although they are interlinked with traditional methods, they save a lot of code. Or that sentence-"jquery makes JavaScript code simple!" ”
1. addclass ()-Join? CSS Classes
$ ("#target"). AddClass ("Newclass");
#target refers to the ID of the element that needs to be added? style
Newclass refers to the name of a CSS class
2. 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
3. Toggleclass ()-join? or remove a CSS class: assuming the CSS class already exists, it will be removed; instead, suppose the CSS class does not exist, it will be added.
$ ("#target"). Toggleclass ("Newclass")
Assuming that the element with ID "target" already has a CSS style defined, it will be removed;
Conversely, the CSS class "Newclass" will be assigned to that ID.

In practice, we often define these CSS classes first, and then change the page element style through JavaScript event triggering, such as clicking on a link. In addition, jquery provides a method Hasclass ("ClassName") to infer whether an element has been assigned to a CSS class.

The following is a complete example.

<! DOCTYPE html>


CSS styles for JavaScript manipulation elements

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.