[JavaScript] There are two ways to import jquery: local import and online import.

Preface

jQuery is a library used to quickly write front-end scripting languages ​​instead of JavaScript. jQuery can greatly simplify complex js codes, allowing developers to focus on achieving page effects.

There are two ways to import

There are two ways to import jQuery, one is local import, and the other is import using hyperlink.

Method 1: Local import

Insert image description here

  • Click the link below

Insert image description here

  • You can see the source code of jquery, as follows:

Insert image description here

  • Direct ctrl+A, select all jQuery source codes, create a new txt file in VScode, copy the source code into it, and then change the suffix to .js.

Insert image description here

  • Then, introduce this .js file into the page where you want to use jquery. The code is as follows:
<script src="你的.js文件路径"></script>
  • Write jquery code here
<script>
	//在此书写你的jQuery代码
</script>
  • Note that you must first introduce the jQuery source code, and then introduce the jQuery code you wrote, because the loading order of the page is from top to bottom. If the browser loads your jQuery code first and then the jQuery library, your jQuery code will It is considered to be an incorrect writing format and the effect cannot be achieved.

Method 2: There are two ways to import online

  • Before using the jQuery library, you need to import it. There are two ways to introduce the jQuery library online: download it directly from the official website or download it from a CDN.

1. Download from the official website:

  • Before downloading the jQuery library, you need to visit the official website and select the version you want to download. Then add the following in the HTML code:
<script src="jquery-3.6.0.min.js"></script>
  • This will load the jQuery library from the file system and add it to the HTML page.

2. Download from CDN:

  • Many large Internet companies provide CDN services, including Google, Microsoft, jQuery and CDNJS, etc.
  • Here we take Google CDN as an example and add the following content to the HTML code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  • This will download the jQuery library from Google CDN servers and add it to the HTML page.
Replenish
  • Import the online jQuery code by writing a URL in the script's src attribute.
  • There are many websites that use jQuery now. You can find a jQuery code link on other websites.
  • The browser will pre-download jQuery when loading a website that previously used jQuery, so there is no need to download it again when we get here. Even if our jQuery version is a new version that has not been loaded by the browser, the jQuery code download is still It will go very quickly
  • However, if you are still worried about affecting loading speed, importing jQuery files locally is indeed the best way.
  • Import online jQuery:
<script src=""></script>
<script>
	//在此书写你的jQuery代码
</script>

Guess you like

Origin blog.csdn.net/qiqizgl777/article/details/132214798