JQuery three methods for operating element css styles

Source: Internet
Author: User

We often use Javascript to change the style of page elements. One of the methods is to change the CSS Class of page elements. In traditional Javascript, we usually implement it by processing the classname feature of HTML Dom; jQuery provides three methods to implement this function. Although they share the same idea with traditional methods, they save a lot of 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.

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 method, hasClass ("className"), to determine whether an element has been assigned to a CSS class.

The following is a complete example.
Copy codeThe Code is as follows:
<! Doctype html>
<Head>
<Title> flashing images </title>
<Style type = "text/css">

@-Webkit-keyframes twinkling {/* transparency from 0 to 1 */
0% {
Opacity: 0;/* Transparency: 0 */
}
100% {
Opacity: 1;/* Transparency: 1 */
}
}

. Up {
-Webkit-animation: twinkling 1 s infinite injection-in-out;
}
</Style>

</Head>
<Body>
<Div id = "soccer" class = "up">
Haha
</Div>
<Br/>
<Input type = "button" onclick = 'btnclick () '>
<Script src = "./jQuery/jquery-1.8.3.js" type = "text/javascript"> </script>
<Script>

Function btnClick (){
// $ ("# Soccer"). removeClass ("up ");
$ ("# Soccer"). toggleClass ("up ");
// $ ("P: first"). removeClass ("intro ");
}
</Script>
</Body>
</Html>

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.