jquery add a class to the Li tag after clicking on the Li tag and delete the previous Li's class when clicking on the next Li

Source: Internet
Author: User

Idea: After clicking on the current LI element, use Removeclass () to remove all sibling elements (using siblings () to get the class style, and then use AddClass () to add class to the current Li

This is illustrated below:

1. HTML structure: Design of three Li elements

<ul id= "Test" >    <li>Glen</li>    <li>Tane</li>    <li>John</li> </ul>

2, CSS style: Design a class selected, indicating the effect after selection

<style>    . Selected{font-weight:bold; background: #ff99cc;  Color:#fff;} </style>

3. jquery Code:

$ (function() {    $ ("#test li"). Click (function() {        $ (this). Siblings (' Li '). Removeclass (' selected ');  // Remove styles from other sibling elements         $ (this). AddClass (' selected ');                            // add a style for the current element    

jquery add a class to the Li tag after clicking on the Li tag and delete the previous Li's class when clicking on the next Li

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.