Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';动态加载jquery后,请不要使用它_Javascript_Jquery_Html - Fatal编程技术网

Javascript Can';动态加载jquery后,请不要使用它

Javascript Can';动态加载jquery后,请不要使用它,javascript,jquery,html,Javascript,Jquery,Html,我正在将data.js加载到我的a.html页面中 在我的data.js中,我检查a.html是否有jquery,如果没有,则加载它。在加载jquery之后,我正在进行一个ajax调用,但是我得到了 $ is not defined 例外 这是我的data.js if(typeof jQuery=='undefined') { var headTag = document.getElementsByTagName("head")[0]; var

我正在将data.js加载到我的a.html页面中

在我的data.js中,我检查a.html是否有jquery,如果没有,则加载它。在加载jquery之后,我正在进行一个ajax调用,但是我得到了

$ is not defined
例外

这是我的data.js

if(typeof jQuery=='undefined') {
            var headTag = document.getElementsByTagName("head")[0];
            var jqTag = document.createElement('script');
            jqTag.type = 'text/javascript';
            jqTag.src = 'http://localhost:8001/jquery-min.js';
            headTag.appendChild(jqTag);
        }

        var url = 'http://127.0.0.1:5000';

        $.ajax({
            url: url,
            success: function(data) {
               $("#" + containerId).html(data);
            },
            error: function(e) {

               console.log(e.message);
            }
        });
从开发人员控制台,我可以看到jquery已加载。我可以在参考资料和head标记中看到它。

在进行AJAX调用时,jQuery(和$THES)还没有加载,因为您只是将脚本标记添加到DOM中。然后开始下载。相反,您应该绑定script标记的onload事件,并在加载后执行操作

范例

jqTag.onload = function() {
    var url = 'http://127.0.0.1:5000';

    $.ajax({
        url: url,
        success: function(data) {
           $("#" + containerId).html(data);
        },
        error: function(e) {

           console.log(e.message);
        }
    });
}
jqTag.src = 'http://localhost:8001/jquery-min.js';
更改此行:

if(typeof jQuery=='undefined')


在开发人员控制台中,您是否收到任何错误?您的ajax调用正在加载jQuery文件之前运行。@adeneo我收到的$is not defined error,它已经存在问题。您应该对ajax脚本执行相同的操作。它将异步拉取文件。当您到达
$.ajax
调用时,它不会被加载。您可以查看
script
元素的
readyState
onload
事件并添加回调。我如何理解它已加载?
typeof
始终返回一个字符串,这样做条件就永远不会为真?
if(typeof jQuery== undefined ) {