toggleClass and remove class from all other elements

前端 未结 6 533
感动是毒
感动是毒 2020-12-01 03:18

How can I toggleClass and remove class from all other elements? Consider a div that contains a tags: html:


                      
相关标签:
6条回答
  • 2020-12-01 03:28

    I'm assuming that you have a class 'size' that contains a number of elements of which only one should be able to have the class 'checked' at any one time.

    $(".size a").click(function()
    {
        if($this).hasClass('checked')
        {
            $(".size a").removeClass('checked');
            $(this).addClass('checked');
        }
        else
        {
            $(this).removeClass('checked');
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:29

    This will do it

     $(".size a").click(function(){
        $('.size a.checked').not(this).removeClass('checked');
        $(this).toggleClass('checked');
     })
    

    Update

    Alternatively you could do

     $(".size").on('click','a', function(){
        $(this).toggleClass('checked').siblings().removeClass('checked');
     })
    
    0 讨论(0)
  • 2020-12-01 03:33

    Only You need to Use it. :)

    <script>
        $(document).ready(function () {
            $('.demo_class').click(function () {
                $(this).toggleClass('active').siblings().removeClass('active');
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-01 03:33
    $('.size a').on('click', function (e) {     
                        e.preventDefault();
    
                        $(this).removeClass("checked");
                        $(this).slideToggle();  
                     $(this).toggleClass('checked').removeClass('checked');
    
                    });
    
    0 讨论(0)
  • 2020-12-01 03:38

    So late but its my answer

    $(".size").click(function(){ $('.size').removeClass('checked') //Clear all checked class on .size $(this).addClass('checked') //Add checked class to current clicked .size })

    Its cleaner,powerfull and safe.

    0 讨论(0)
  • 2020-12-01 03:39
    <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>On click Change Css</title>
        <link rel="stylesheet" href="css/style.css" >
        <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
        <script>
            $(document).ready(function(){
                $('.main').click(function(){
                    $(this).toggleClass('main1').siblings().removeClass('main1');;
                 });
            });
        </script>
    
    </head>
    
    <body>
    
        <center>
            <div class="main" id="main-box">
                <h2>This is main Heading</h2>
                <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
                </p>
            </div>
            <div class="main">
                <h2>This is main Heading</h2>
                <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
                </p>
            </div>
            <div class="main">
                <h2>This is main Heading</h2>
                <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
                </p>
            </div>
        </center>
    
    </body>
    

    Css

    .main {
      width:300px;
      margin:30px;
      margin:20px 25px;
      text-align:center;
      background-color:#CCC;
      padding:20px;
      display:inline-block;
    }
    .main:hover {
      cursor:pointer;
    }
    .main1 {
      width:300px;
      margin:30px;
      margin:20px 25px;
      text-align:center;
      background-color:#CCC;
      padding:20px;
      border:3px solid #036;
    }
    
    0 讨论(0)
提交回复
热议问题