jquery attribute Operation AddClass removeclass hasclass Toggleclass ()

Source: Internet
Author: User


Reference page:

Http://www.w3school.com.cn/jquery/jquery_ref_attributes.asp

1:addclass () Romoveclass (): Adds the specified class name to the matching element one or more, lethal horizon one or more class attributes: (if you add more than one class, you can separate the class name with a space)

A: To add a class to the first P element:$ ("P:first"). AddClass ("Intro");

B: Adding a class using a function

<script type="Text/javascript">
$ (document). Ready (function () {
$("Button"). Click (function () {
$('P'). addclass (function (n) {
return'Par_'+ N;
});
});
});
</script>
<style type="Text/css">
. par_0
{
Color:blue;
}
. par_1
{
color:red;
}
</style>

C : to in the selected element Add two classes: Multiple classes are separated by spaces to :

$ ("P:first"). AddClass ("intro note");

D: to change the class of an element:  use AddClass () and Removeclass () to remove the class and add a new class.

$ ("P:first"). Removeclass ("Intro"). AddClass (' main ');


2:hasclass ():checks whether the selected element contains the specified class containing the return true: does not contain return false

          Alert ($ ("P:first"). hasClass("intro") );


3:toggleclass () : Toggles one or more classes that set or remove the selected element : Multiple switches possible

         $ ("P"). toggleClass("main") ;






jquery attribute Operation AddClass removeclass hasclass Toggleclass ()

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.