CSS style for jquery operation elements _jquery

Source: Internet
Author: User

We often use JavaScript to change the style of page elements. One way to do this is to change the page element

Vegetarian CSS Class (Class), which in traditional JavaScript, we usually do by processing HTML

ClassName features of the DOM, and jquery provides three ways to do this,

Although they are connected to traditional methods, they save a lot of code. or the sentence-

"jquery makes JavaScript code concise!" ”

1. addclass () – Add CSS Class

Copy Code code as follows:

$ ("#target"). AddClass ("Newclass");
#target refers to the ID of the element that needs to add a style
Newclass refers to the name of the CSS class

2. Removeclass () – Remove CSS Class

Copy Code code as follows:

$ ("#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 the CSS class

3. Toggleclass () – Add or remove CSS classes: If the CSS class already exists, it will be removed;

Conversely, if the CSS class does not exist, it will be added.

Copy Code code as follows:

$ ("#target"). Toggleclass ("Newclass")
If an element with the ID "target" already has a CSS style defined, it will be removed;
Conversely, the CSS class "Newclass" will be assigned to that ID.

In practical use, we often define these CSS classes first, and then touch them through JavaScript events

To change the style of the page elements, such as clicking on a link. In addition, jquery also provides a side

The method Hasclass ("ClassName") is used to determine whether an element has been assigned to a CSS class.

The following is a complete example.

Copy Code code as follows:

<span style= "FONT-SIZE:18PX;" ><! DOCTYPE html>
<title> Picture Flashing </title>
<style type= "Text/css" >
@-webkit-keyframes twinkling{/* Transparency from 0 to 1*/
0%{
opacity:0; /* Transparency is 0*/
}
100%{
Opacity:1; /* Transparency is 1*/
}
}
. up{
-webkit-animation:twinkling 1s infinite ease-in-out;
}
</style>
<body>
<div id= "Soccer" class= "up" >
Ha ha
</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>
</span>

The above mentioned is this article about the jquery operation CSS style all content, hoped everybody can like.

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.