Home > Web Front-end > JS Tutorial > Summary of jQuery errors and solutions

Summary of jQuery errors and solutions

小云云
Release: 2018-01-22 11:14:40
Original
2276 people have browsed it

Have you ever encountered the problem of jQuery is not define? When testing a program today, jQuery has obviously been loaded but it always prompts that jQuery is not defined. After many tests, the problem was finally discovered. Here is a brief summary. Friends who need it can refer to it. I hope it can help everyone.

Usually there are several solutions to this situation:

1: Check whether the jquery file is imported

Even if the file is introduced, is it passed? Problems may occur when downloading software such as full-site downloaders. It is recommended to use Thunder to download from the official website.

2: To check whether the path is wrong, you can click the js file path in the page source code.

Sometimes some paths will be automatically added to js, ​​causing path errors

3: JS is an interpreted language, which is executed sequentially based on tag references. $ is an object generated in jQuery. If it needs to be used, the jquery.js file must be placed in the JS that uses it. Front.

Place the jQuery library before the JavaScript scripts that depend on jQuery, and put these codes into document.ready to ensure that the DOM is loaded.


<script type="text/javascript" src="path/to/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
  //依赖于jQuery的代码
 });
</script>
Copy after login

4. Sometimes we use a third-party cdn’s js to load the CDN’s jQuery and it fails or times out.

When provided There is a problem with jQuery's CDN that causes jQuery to fail to load, or the browser times out when loading the jQuery file due to network problems, and a jQuery undefined error occurs.

Solution: Mount the jQuery file on your own website as a backup , if the CDN fails to load jQuery, use your own website to store jQuery. In this case, most users can still speed up access through CDN, and can avoid errors if there is a problem with the CDN.


<script src="https://cdn.staticfile.org/jquery/3.1.1/jquery.min.js"></script>
<script> 
window.jQuery || document.write(&#39;<script src="/js/jquery.min.js"><\/script>&#39;))
</script>
Copy after login

There will be problems whether it is downloaded through some software such as a whole-site downloader. It is recommended to use Thunder to download from the official website.

Related recommendations:

Analysis on preventing recursive stack overflow errors in JavaScript

Solving the problem of errors when unique indexes are repeated in thinkphp ( Figure)

mysql Implementation method of continuing execution after an error occurs in the batch file_MySQL

The above is the detailed content of Summary of jQuery errors and solutions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template