refresh DOM after append element

前端 未结 5 1350
忘了有多久
忘了有多久 2020-12-09 16:24



        
5条回答
  •  星月不相逢
    2020-12-09 16:59

    You need to use event delegation for dynamically added element.

    $(document).ready(function() {
        $('#main_body').append("

    Hello

    Click"); $('#main_body').on('click', '#but', function() { alert( "bla bla" ); }); });

    Event delegation allows us to attach a single event listener, to a parent element, that will fire for all children matching a selector, whether those children exist now or are added in the future

提交回复
热议问题