How to introduce jquery into a js file

WBOY
Release: 2023-05-18 16:04:08
Original
6153 people have browsed it

In Web development, jQuery is a very popular JavaScript library, which greatly simplifies writing JavaScript code and manipulating DOM without having to consider too much compatibility issues. When completing the development of a project, we usually use jQuery to speed up development efficiency and improve the maintainability and stability of the project. So, how to introduce jQuery into a JS file? This article will detail two common methods.

Method 1: Introduce through CDN

CDN (Content Delivery Network) is a group of servers scattered in different places that can cache and accelerate the transmission of website content, making the website load faster. jQuery has some CDN service providers (such as Google, Microsoft, Baidu, etc.), and we can introduce jQuery through the links they provide.

Taking Google CDN as an example, we can copy and paste the following code into the HTML file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Copy after login

In this code, the "src" attribute specifies where to load the jQuery code. When the page loads, the browser will automatically download jQuery from the Google server and cache it instead of downloading it from our server, which can greatly improve the page loading speed.

When we write JS code, we only need to use the "$" symbol in the corresponding JS file, such as:

$(document).ready(function() {
    // ...
});
Copy after login
Copy after login

At this time, we have successfully introduced jQuery, which can be passed $ Symbols refer to various functional functions provided in jQuery.

Method 2: Import through files

In addition to importing jQuery from CDN, we can also download jQuery locally and import it through files. The specific method is as follows:

  1. Visit the jQuery official website (https://jquery.com/) and download the latest jQuery compressed file.
  2. Decompress the downloaded file locally, for example, copy jquery.min.js to the "js" folder.
  3. Add the following code in the HTML file:
<script src="js/jquery.min.js"></script>
<script src="js/myscript.js"></script>
Copy after login

The first line of code here specifies loading the jQuery script, while the second line of code specifies the JS script we wrote ourselves, for example , "myscript.js".

  1. In the myscript.js file, use the $ symbol to introduce jQuery, for example:
$(document).ready(function() {
    // ...
});
Copy after login
Copy after login

It should be noted here that when using the file to introduce jQuery, we need to ensure The jQuery files are referenced correctly and the order of the code is also important. If we put our own JS files before jQuery, the code will report an error.

The above are two common ways to introduce jQuery. When we need to use jQuery, we only need to choose a way that suits us, and then we can happily write maintainable and stable web applications.

The above is the detailed content of How to introduce jquery into a js file. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!