简体   繁体   English

在使用jQuery添加类之后,如何通过单击功能删除该类

[英]How to remove a class with click function after it has been added with jquery

I am new to jquery but I have been learning a lot lately. 我是jQuery的新手,但最近学到了很多东西。 I am trying to add a class to a div with a click handler and then remove it when it is selected. 我试图通过单击处理程序将一个类添加到div,然后在选择它时将其删除。 Not sure if this is the best method as I am having trouble with it. 不确定这是否是最好的方法,因为我遇到了麻烦。 Maybe toggleClass would be a better solution? 也许toggleClass是更好的解决方案?

Here is the html: 这是html:

   <nav>
        <div class="container">
            <a class="toggleMenu" href="#">Menu</a>
                <ul class="nav">
                    <li><a href="index.html">Barnes</a></li>

                    <li><a href="craig.html">Craig</a></li>

                    <li><a href="bennie-donna.html">Bennie & Donna</a></li>

                    <li><a href="peggy.html">Peggy</a></li>

                    <li><a href="tina.html">Tina</a></li>

                    <li><a href="bennie.html">Bennie</a></li>

                    <li><a href="debbie.html">Debbie</a></li>

                    <li><a href="david.html">David</a></li>

                </ul> 
        </div>  
    </nav> 

<div class="photos-wrap">
        <div class="photos">
            <img src="images/bennie/image1.jpg" alt="pic"/>
            <img src="images/bennie/image2.jpg" alt="pic"/>
            <img src="images/bennie/image3.jpg" alt="pic"/>
            <img src="images/bennie/image4.jpg" alt="pic"/>
            <img src="images/bennie/image5.jpg" alt="pic"/>
            <img src="images/bennie/image6.jpg" alt="pic"/>
            <img src="images/bennie/image7.jpg" alt="pic"/>
            <img src="images/bennie/image8.jpg" alt="pic"/>
            <img src="images/bennie/image9.jpg" alt="pic"/>
            <img src="images/bennie/image10.jpg" alt="pic"/>
        </div>
    </div>

Here is the minimal css: 这是最小的CSS:

.photos-wrap {
    position:absolute;
    top:400px;
}

.photos {
    margin:0 auto;
    width:60%;
    padding-bottom: 300px;
}

.photos img {
    position: relative;
    margin:-20px;
}

.toggleMenu {
    display:  none;
    background: #175E4C;
    text-decoration: none;
    padding: 10px 15px;
    color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.8);
    border: rgba(0, 0, 0, 0.8);

     -webkit-border-radius: 5px;
     -moz-border-radius: 5px;
     border-radius: 5px;

     background-image: linear-gradient(bottom, #175E4C 45%, #7BB5A5 100%);
    background-image: -o-linear-gradient(bottom, #175E4C 45%, #7BB5A5 100%);
    background-image: -moz-linear-gradient(bottom, #175E4C 45%, #7BB5A5 100%);
    background-image: -webkit-linear-gradient(bottom, #175E4C 45%, #7BB5A5 100%);
    background-image: -ms-linear-gradient(bottom, #175E4C 45%, #7BB5A5 100%);

    background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0.45, #175E4C),
    color-stop(1, #7BB5A5)
);
}

.drop-down {
    margin-top:280px;


And lastly the jquery:

$('.toggleMenu').click(function(){
    $('.photos-wrap').addClass('drop-down');

    }); 

This enables me to add the class 'drop-down' to the .photos div by clicking on the .toggleMenu. 这使我可以通过单击.toggleMenu将类“下拉列表”添加到.photos div中。 I want to remove the 'drop-down' class when the .toggleMenu is clicked again. 我想在再次单击.toggleMenu时删除“下拉”类。

Anyone know how to accomplish this? 有人知道如何做到这一点吗? Need help. 需要帮忙。

Give this a shot: 试一下:

    $('.toggleMenu').click(function(){
        $('.photos-wrap').toggleClass('drop-down');
    }); 

Interestingly, you mention this in your post. 有趣的是,您在帖子中提到了这一点。 You should listen to yourself! 你应该听你自己!

You can try with toggleClass() : 您可以尝试使用toggleClass()

$('.container').on('click', '.toggleMenu', function(){
   $('.photos').toggleClass('drop-down');
});

In your click handler, just use toggleClass() instead of addClass() 在点击处理程序中,只需使用toggleClass()而不是addClass()

See 看到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM