Javascript - Difference between $(document).on(“click”, “selector”, function …); and $(“selector”).on(“click”, function …);

前端 未结 4 1753
抹茶落季
抹茶落季 2021-01-15 20:15

What is difference between these two codes ?

$(document).on(\'click\', \'.close_modal_window\', function(){
        alert(\"window closed\");
    });
         


        
4条回答
  •  不要未来只要你来
    2021-01-15 21:00

    first it works if you have static html which you write like

    My div

    second it works if you make/create dynamic html like
    var myDiv ='

    My div
    '
    and then you append it in some element like

    $('#someid').append(myDiv )
    the second approach is called event delegation

提交回复
热议问题