简体   繁体   English

无法使用jQuery从元素中删除类

[英]Can't remove class from an element using jQuery

I have following markup: 我有以下标记:

<div class="row">
    <img src="image.png" class="download-image regular-image" />
<div class="options">
     <p>download</p>
</div>

<img src="image.png" class="download-image regular-image" />
<div class="options">
     <p>download</p>
</div>

ANd following code to manipulate it (it changes image, basically it's an active state implementation): 和以下代码对其进行操作(它会更改图像,基本上是一个活动状态的实现):

$(".download-image").click(function (event) {
    event.stopPropagation();
    $(this).next().toggle();
    $('.download-image').removeClass('current').attr('src', 'image.png');
    $(this).addClass("current").attr('src', 'image2.png');
    $(document).delegate('.current','mouseover',function() {
        $(this).attr("src", "image2.png");
    });
    $(document).delegate('.current','mouseout',function() {
        $(this).attr("src", "image.png");
    });
});

This code works well, but I'm having a trouble removing .current class by clicking on an element. 这段代码很好用,但是在单击元素时删除.current类时遇到麻烦。 Althrough I implemented it via clicking on any place of the page like this: 一直以来,我都是通过单击页面的任意位置来实现的,如下所示:

$("html").click(function() {
    $(".options").hide();
    $(".download-image").removeClass('current').attr("src", "image.png");
    $(".download-image").hover(function() {
        $(this).attr("src", "image2.png");
    }, function() {
        $(this).attr("src", "image.png");
    });
 });

This chunk does exactly what I want, but I also wanna have same behavior for active image too. 该块完全符合我的要求,但我也想对活动图像也具有相同的行为。 What I tried: 1. Add .current to html click event 2. Create same function but for .current class 3. Use .on('click', '.current', function() 4. Going just like this: 我尝试了什么:1.将.current添加到html click事件中2.创建与.current类相同的函数3.使用.on('click', '.current', function() 4.如下所示:

$('.current').click(function() {
     $(this).removeClass('current');
}

Nothing above worked for me. 以上对我没有任何帮助。 Where I was wrong? 我哪里错了? Any help would be appreciated. 任何帮助,将不胜感激。

The this keyword in the callback function of a listener has the event object attached to it. 侦听器的回调函数中的this关键字已附加事件对象。 You want the HTML element. 您需要HTML元素。 You can see this in action by logging out the value of this inside of the callback and looking at the object. 通过在回调内部注销this的值并查看该对象,可以看到实际的效果。 Try this to get the HTML element instead: 尝试使用以下方法获取HTML元素:

$('.current').on('click', function(e) {      
       $(e.target).removeClass('current');
 } 

Make note of the argument I'm giving to the callback. 记下我给回调函数提供的参数。 I believe this.target might actually be the same object but I'm on mobile atm so can't verify. 我相信this.target实际上可能是同一对象,但是我在移动atm上,因此无法验证。 You can though, by logging out the values with a console.log(this); 但是,您可以通过使用console.log(this);登出值来进行操作。 statement 声明

Solution was so obvious that I feel ashamed I haven't tried it before posting my question. 解决方案是如此明显,令我感到as愧的是,在发布问题之前我还没有尝试过。 I used .toggleClass() method and now my click() event looks like this: 我使用了.toggleClass()方法,现在我的click()事件如下所示:

$(".download-image").click(function (event) {
    event.stopPropagation();
        $(this).next().toggle();
        $('.download-image').removeClass('current').attr('src', 'image.png');
        $(this).toggleClass("current").attr('src', 'image2.png');
        $(document).delegate('.current','mouseover',function() {
            $(this).attr("src", "image2.png");
        });
        $(document).delegate('.current','mouseout',function() {
            $(this).attr("src", "image2.png");
        });
    });

Please note that I use old jQuery, if you're using v3+ , you should replace .delegate() method with .on() as reffered in comments. 请注意,我用老的jQuery,如果你使用的v3+ ,则应更换.delegate()方法用.on()作为下文称的意见。

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

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