addClass and removeClass in jQuery - not removing class

后端 未结 10 1583
不知归路
不知归路 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:42

    Whenever I see addClass and removeClass I think why not just use toggleClass. In this case we can remove the .clickable class to avoid event bubbling, and to avoid the event from being fired on everything we click inside of the .clickable div.

    $(document).on("click", ".close_button", function () { 
        $(this).closest(".grown").toggleClass("spot grown clickable");
    });  
    
    $(document).on("click", ".clickable", function () {
        $(this).toggleClass("spot grown clickable");
    }); 
    

    I also recommend a parent wrapper for your .clickable divs instead of using the document. I am not sure how you are adding them dynamically so didn't want to assume your layout for you.

    http://jsfiddle.net/bplumb/ECQg5/2/

    Happy Coding :)

提交回复
热议问题