頁面不刷新,只重新加載js文件 function 方法

/**
                                 * 重新加載下js文件
                                 * @param id
                                 * @param newJS
                                 * @returns {boolean}
                                 */
                                function reloadAbleJSFn(id, newJS = false)
                                {

                                    var oldJs = jQuery('#' + id);
                                    // 沒找返回下
                                    if (oldJs.length < 1)

                                        return false;

                                    // 未傳輸新的js路徑 就用舊的
                                    if (!newJS)

                                        newJS = jQuery(oldJs).attr('src');

                                    var parent = jQuery(oldJs).parent();
                                    jQuery(parent).find('#' + id).remove();
                                    var scriptObj = document.createElement("script");
                                    scriptObj.src = newJS;
                                    scriptObj.type = "text/javascript";
                                    scriptObj.id = id;
                                    jQuery(parent).append(scriptObj);
                                }

參考:https://blog.csdn.net/wudean0910/article/details/84980979

代碼已經過修改。。。不知算原創嗎。

有前端問題的童鞋歡迎加羣討論:

115658205

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章