jQuery ajax 加载不是函数

jQuery ajax load not a function

这个例子

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<div id="test"></div>
<script>
$(document).ready(() => {
  $('#test').load('doesntmatter');
});
</script>

在我看来它与 ajax load function 的示例相同。正如代码片段可以告诉您的那样,它实际上出错了

Uncaught TypeError: $(...).load is not a function

我做错了什么?

JQuery格式错误:

$(document).ready(function() {
        $('#test').load('doesntmatter');
    });

然后将目录中的页面名称等添加到加载参数中

  • 还要确保您的脚本是最新的功能版本

https://code.jquery.com/jquery-3.2.1.slim.min.js is the slim edition of jquery, which does not include ajax. slim is the default version included in an Express server. Use the full version of jquery at https://code.jquery.com/jquery-3.2.1.min.js

请试试这个

$(document).ready(function(){
    $("button").click(function(){
       $("#div1").load("demo_test.txt #p1"); 
    });
});

尽量不使用JQuery:

这将确保在使用前加载 JQuery。

window.addEventListener("load", function(event) {
  $('#preloader').delay(400).fadeOut(500);
  // Do what you want, the window is entirely loaded and ready to use.
});

加载整个页面时触发加载事件,包括样式表图像等所有依赖资源。这与 DOMContentLoaded 形成对比,后者在页面 DOM 加载后立即触发,无需等待资源完成加载。

Mozilla 文档:load event

编辑:根据问题不要混淆 window.loadedjquery.load

首先,将jquery.slim更改为jquery,如之前的回复

其次,将本机事件处理程序用于现代浏览器的最佳实践(我认为)

// To be sure $ is defined
// Window loaded event
window.addEventListener("load", function(event) {

  // Now $ or JQuery is completly available
  // Now using JQuery.load() should be defined
  $('#test').load('doesntmatter');

  // Do what you want, the window is entirely loaded and ready to use.
});

首先,请确保您没有在 js 代码后包含 jquery 库 ! 使用 bootstrap!

时要特别小心

我遇到了问题 - 我没有在 footer.php 中看到 bootstrap 代码的一部分,它还有另一个 jquery库 - 导致了问题。

这是 jquery 下载的屏幕截图 jquery download page (https://jquery.com/download/)
这条线很容易被忽略。
由于很多人都在使用 jquery ajax,也许他们应该将文件重命名为 jquery-slim-no-ajax.js

这是个好建议,我遇到了与 jquery .load 函数和其他未加载函数相同的问题,因此请确保您在每个页面中使用相同的 jquery 路径,(相同版本),它会很好地工作。

我在您的 HTML 中找到 link 到 post 的脚本,其中包含 ajax 库:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">

试试吧。加载功能有效。

jQuery ajax 加载不是函数

下面使用URL
源码='https://code.jquery.com/jquery-3.2.1.min.js'