简体   繁体   English

为什么我的 $(document).ready 有问题?

[英]Why I have problem with $(document).ready?

I'm learning jQuery but I have a problem.我正在学习 jQuery,但我遇到了问题。

Here the code:这里的代码:

var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.4.1.min.js';
script.type = "text/javascript";

var pt = document.createElement('script');
pt.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
document.getElementsByTagName('head')[0].appendChild(pt);

$(document).ready(function() {
  alert("ti vedo");
  $.post('wait.php', function(data) {
    alert("ti vedo2");
    var dati = JSON.parse(data);
    alert(data);
    for (var i = 0; i <= dati.length; i++) {
      var node = $(document.createElement("span"));
      var content = $(document.createElement("div"));
      var code = dati[i];
      var text = $(document.createTextNode(code));
      node.append(text);
      content.append(node);
      $("#boxMessage").append(content);
    }
  });
});

I use alert to check the error, but I can't see the first alert.我使用警报来检查错误,但我看不到第一个警报。 So the problem is $(document)... but I don't understand why.所以问题是 $(document)... 但我不明白为什么。 I'm making a mistake when I include the jQuery library?我在包含 jQuery 库时犯了一个错误? Thank you for the Help!感谢您的帮助!

You're getting the error because you're trying to access jQuery (via $ ) before it's available.您收到错误是因为您试图在它可用之前访问jQuery (通过$ )。

You can run your jQuery-related code after the script was really loaded:您可以在脚本真正加载后运行与 jQuery 相关的代码:

 var script = document.createElement('script'); script.src = 'https://code.jquery.com/jquery-3.4.1.min.js'; script.type="text/javascript"; document.getElementsByTagName('head')[0].appendChild(script); script.addEventListener('load', function(){ alert("ti vedo"); $.post('wait.php',function(data){ alert("ti vedo2"); var dati=JSON.parse(data); alert(data); for(var i=0; i<=dati.length; i++){ var node=$(document.createElement("span")); var content=$(document.createElement("div")); var code=dati[i]; var text=$(document.createTextNode(code)); node.append(text); content.append(node); $("#boxMessage").append(content); } }); });

Also, you don't need two different versions of jQuery simultaneously.此外,您不需要同时使用两个不同版本的jQuery

The order of activities is:活动顺序为:

  1. A script element to load jQuery 3.4.1 is added to the DOM加载 jQuery 3.4.1 的脚本元素被添加到 DOM
  2. A script element to load jQuery 3.5.1 is added to the DOM加载 jQuery 3.5.1 的脚本元素被添加到 DOM
  3. The $ function is called, but $ is undefined, so the script terminates with an exception调用了$函数,但$未定义,因此脚本以异常终止
  4. jQuery 3.4.1 is loaded, adding $ to the environment加载 jQuery 3.4.1,在环境中添加$
  5. jQuery 3.5.1 is loaded, overwriting $ jQuery 3.5.1 已加载,覆盖$

Use a regular <script> element to load your dependencies instead of trying to load them half way through executing your script.使用常规<script>元素加载您的依赖项,而不是尝试在执行脚本中途加载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM