Function doesn't work after appending new element

后端 未结 1 658
长发绾君心
长发绾君心 2020-12-03 13:23

I have a problem with the \"click\" action on a new element created by a jQuery function, it doesn\'t work.

I created a JSFiddle to this link (JSFiddle), but it does

相关标签:
1条回答
  • 2020-12-03 13:48

    jQuery is only aware of the elements in the page at the time that it runs, so new elements added to the DOM are unrecognized by jQuery. To combat that use event delegation, bubbling events from newly added items up to a point in the DOM that was there when jQuery ran on page load. Many people use document as the place to catch the bubbled event, but it isn't necessary to go that high up the DOM tree. Ideally you should delegate to the nearest parent that exists at the time of page load.

    $(".pulsanteIscrizioni").on("click", function()
    

    is what you have now. Change it to -

    $(document).on("click", '.pulsanteIscrizioni',function()
    
    0 讨论(0)
提交回复
热议问题