.click() fails after dom change

后端 未结 3 868
逝去的感伤
逝去的感伤 2020-12-20 21:22

I searched on web but I didn\'t find any answer since this \"problem\" is not the usual one about the difference between .on() and .click(). With jquery 2.1.3 the click func

3条回答
  •  忘掉有多难
    2020-12-20 21:47

    But this works only if I use .on().

    First of all , you should realize that :

    If

    $('#button2').click(function() {
        alert(0); 
    });
    

    comes after

    $('#button1').click(function() {
        $('div').html("

    Hello

    "); });

    like :

       $('#button1').click(function() {
            $('div').html("

    Hello

    "); $('#button2').click(function() { alert(0); }); });

    Then it WILL work.

    the thing which you did in the last code is attaching the handler to the body element which is working because of event propagation.

    your code is working beacuse on allows you to do selector matching + attaching single handler to the body element.

提交回复
热议问题