jquery dynamically additions or deletions to an element class or attribute

Source: Internet
Author: User

Background: Add and delete classes or attributes to HTML elements dynamically through jquery, so that HTML elements present different styles at different times, giving the user a better sense of experience.

If the following P fragment and button buttons are present, the code is as follows:

1         <p id="pdisplay"> now the background color is white, click the button after the background changed to red </p>2         <button id= " ChangeColor "> Change Colors </button>

The following CSS code exists:

1     <style>2         . colorred{3         background-color:red; 4     }5     </style>

The following JS code exists:

        $ ("#changeColor"). Toggle (function () {            $ ("#pDisplay "). AddClass ("colorred");        },        function () {            $ (" #pDisplay "). Removeclass ("colorred");        });

As the code above, when the button is clicked, add or remove the CSS class colorred to the $ ("#pDisplay") element as follows:

Similarly, it can be applied to attr () and removeattr () with the following code:

1 //Disabled settings element is not available:2 3$( This). attr ("Disabled","Disabled")4 5 //Remove the diasble attribute from the push element:6 7$("#push"). Removeattr ('Disabled')

jquery dynamically additions or deletions to an element class or attribute

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.