addClass and removeClass in jQuery - not removing class

后端 未结 10 1619
不知归路
不知归路 2020-12-05 02:02

I\'m trying to do something very simple. Basically I have a clickable div \'hot spot\', when you click that it fills the screen and displays some content. I achieved this by

10条回答
  •  难免孤独
    2020-12-05 02:57

    I think you're almost there. The thing is, your $(this) in the "close button" listener is not the clickable div. So you want to search it first. try to replace $(this) with $(this).closest(".clickable") . And don't forget the e.stopPropagation() as Guilherme is suggesting. that should be something like:

    $( document ).ready(function() {   
        $(document).on("click", ".close_button", function () { 
            alert ("oi");
            e.stopPropagation()
            $(this).closest(".clickable").addClass("spot");
            $(this).closest(".clickable").removeClass("grown");
        });  
    
    
        $(document).on("click", ".clickable", function () {
            if ($(this).hasClass("spot")){
                $(this).addClass("grown");
                $(this).removeClass("spot");
            }
        });
    });
    

提交回复
热议问题