jQuery ajax 加载不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46003790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax load not a function
提问by Guillaume CR
This example
这个例子
<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>
seemed to me like it is identical to the examples for the ajax load function. As the code snippet can tell you, it actually errors out with
在我看来,它与ajax 加载函数的示例相同。正如代码片段可以告诉您的那样,它实际上出错了
Uncaught TypeError: $(...).load is not a function
What am I doing wrong?
我究竟做错了什么?
回答by Guillaume CR
https://code.jquery.com/jquery-3.2.1.slim.min.jsis 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
https://code.jquery.com/jquery-3.2.1.slim.min.js是jquery的slim版本,不包含ajax。slim 是 Express 服务器中包含的默认版本。在https://code.jquery.com/jquery-3.2.1.min.js使用完整版的 jquery
回答by Dexter
Here's a screenshot of the jquery downloads from the jquery download page(https://jquery.com/download/)
It is easy NOT to notice this line.
Since many people are using jquery ajax, maybe they should rename the file as jquery-slim-no-ajax.js
这是从 jquery下载页面( https://jquery.com/download/)下载jquery 的屏幕截图。
很容易注意到这一行。
由于很多人都在使用 jquery ajax,也许他们应该将文件重命名为jquery-slim-no-ajax.js
回答by clearshot66
JQuery format is wrong:
JQuery 格式错误:
$(document).ready(function() {
$('#test').load('doesntmatter');
});
then add a page name in your directory or such into the load parameters
然后在您的目录中添加一个页面名称或将其添加到加载参数中
- Also make sure your script is the newest functional version
- 还要确保您的脚本是最新的功能版本
回答by Manoher Kumar
Please try this
请试试这个
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});
回答by Shim-Sao
Try to not use JQueryfor that :
尽量不要为此使用 JQuery:
This will ensure that JQuery is loaded before using it.
这将确保在使用 JQuery 之前加载它。
window.addEventListener("load", function(event) {
$('#preloader').delay(400).fadeOut(500);
// Do what you want, the window is entirely loaded and ready to use.
});
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets images. This is in contrast to DOMContentLoaded, which is fired as soon as the page DOM has been loaded, without waiting for resources finish loading.
load 事件在整个页面加载后触发,包括所有依赖资源,如样式表图像。这与 DOMContentLoaded 形成对比,DOMContentLoaded 在页面 DOM 加载后立即触发,无需等待资源完成加载。
Mozilla documentation: load event
Mozilla 文档:加载事件
Edit : According to the question to not confusing window.loadedand jquery.load
编辑:根据问题不要混淆window.loaded和jquery.load
First, change jquery.slimto jquerylike previous response
首先,像之前的响应一样将jquery.slim更改为jquery
Second, use native event handler for best practice (in my opinion)with modern browsers.
其次,在现代浏览器中使用本机事件处理程序以获得最佳实践(在我看来)。
// 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.
});
回答by Shreyder
First of all, make sure you don't include jquery library after your jscode! Especially be careful if you use bootstrap!
首先,请确保您的 js代码后没有包含 jquery 库!如果您使用引导程序,请特别小心!
I had the problem - I didn't see the part of bootstrap code in the footer.php, which had another jquery library - which caused the problem.
我遇到了问题 - 我没有看到footer.php 中的引导代码部分,它有另一个 jquery 库 - 这导致了问题。