Reload javascript file after an AJAX request

前端 未结 10 727
粉色の甜心
粉色の甜心 2020-12-13 20:25

After the request, the new elements created are not recognized by the event handlers in my jQuery code.

Is there a way to reload the file to re-register these events

10条回答
  •  不知归路
    2020-12-13 21:14

    This codes works perfect for me..

    $("head script").each(function(){
                var oldScript = this.getAttribute("src");
                $(this).remove();
                var newScript;
                newScript = document.createElement('script');
                newScript.type = 'text/javascript';
                newScript.src = oldScript;
                document.getElementsByTagName("head")[0].appendChild(newScript);
            });
    

    It removes the old script tag and make a new one with the same src (reloading it).

提交回复
热议问题