addClass and removeClass in jQuery - not removing class

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

    Use .on()

    you need event delegation as these classes are not present on DOM when DOM is ready.

    $(document).on("click", ".clickable", function () {
        $(this).addClass("grown");
        $(this).removeClass("spot");
    });
    $(document).on("click", ".close_button", function () {  
        $("#spot1").removeClass("grown");
        $("#spot1").addClass("spot");
    });  
    

提交回复
热议问题