jQuery ajax load not a function

2020-02-27 07:13发布

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

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

What am I doing wrong?

6条回答
闹够了就滚
2楼-- · 2020-02-27 07:18

JQuery format is wrong:

$(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
查看更多
beautiful°
3楼-- · 2020-02-27 07:21

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

enter image description here

查看更多
等我变得足够好
4楼-- · 2020-02-27 07:24

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

查看更多
做自己的国王
5楼-- · 2020-02-27 07:26

First of all, make sure you don't include jquery library after your js code! Especially be careful if you use bootstrap!

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.

查看更多
等我变得足够好
6楼-- · 2020-02-27 07:42

Please try this

$(document).ready(function(){
    $("button").click(function(){
       $("#div1").load("demo_test.txt #p1"); 
    });
});
查看更多
放我归山
7楼-- · 2020-02-27 07:45

Try to not use JQuery for that :

This will ensure that JQuery is loaded before using it.

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.

Mozilla documentation: load event

Edit : According to the question to not confusing window.loaded and jquery.load

First, change jquery.slim to jquery like previous response

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.
});
查看更多
登录 后发表回答