The jQuery Local Example

jQuery is a fast and concise JavaScript library primarily designed with a nice motto − Write less, do more. The main purpose of this library is to make it easier to use JavaScript on our website. jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery wraps many lines of JavaScript code into methods that we can call with a single line of code.

There are two ways to link jQuery in an HTML page —

  • By downloading the jQuery library locally — you can download the jQuery file on your local machine and include it in the HTML page.
  • By including the jQuery from a CDN — you can add the jQuery library into your HTML page directly from a Content Delivery Network (CDN).

1. By downloading the jQuery library locally

  • Download the latest version of jQuery library from the official website https://jquery.com/download/. You can download any of the four types of available jQuery versions- uncompressed, minified, slim and slim & minified.
  • Now put the downloaded jquery-3.6.0.min.js file in a directory of your website, e.g. /jquery.
  • We can link jQuery in an HTML page by using a script tag and providing the downloaded jQuery library file address as the src attribute. The jquery-3.6.0.min.js can be added like below.

Let’s understand with the help of a complete example.

Example

In the example below we include jQuery library in our HTML page as follows –

      

Hello

Output

This will produce the following result —

Hello, World! We are using jQuery from local machine

2. By including the jQuery from a CDN

We can link the jQuery library in our HTML page directly from a Content Delivery Network. There are different CDNs that provide the latest version of the jQuery library. For Example, Google, Microsoft, Cloudflare CDNs, and jQuery’s own CDN.

Читайте также:  Css только дочерний элемент

Let’s understand how to link jQuery from these CDNs each with help of examples.

We can link jQuery in an HTML page by using a script tag and providing a jQuery Google CDN address as the src attribute. The jquery.min.js can be added like below.

Here, in the example below we link the jQuery library in our HTML page as follows −

    div    

Output

Here the fadeIn() method changes the opacity, for selected elements, from hidden to visible. It is to specify the speed of the fading effect, which can be slow or fast.

We can link jQuery in an HTML page by using a script tag and providing a jQuery Microsoft CDN address as the src attribute. The jquery-3.6.0.js can be added like below.

Here, in the example below we link the jQuery library from Microsoft CDN in our HTML page −

        

Output

When you click on the “Show Message” button, the message will be displayed.

We can also use Cloudflare CDN to link the jQuery library to our HTML page. To link jQuery in an HTML page we add jQuery Cloudflare CDN address to the src attribute of the script tag. The jquery.min.js can be added like below.

Here, in the example below we link the jQuery library from Cloudflare CDN in your HTML page −

      

Hello

Output

This will produce the following result —

Hello, World! We are using jQuery from Cloudflare CDN.

We can also use jQuery CDN to link the jQuery library in our HTML page. To link jQuery in an HTML page we add the jQuery CDN address to the src attribute of the script tag. We also have to add integrity and crossorigin to the script. We can directly copy the script code from the jQuery website. jquery-3.6.0.min.js can be added like below-

Here, in the example below we link the jQuery library from jQuery CDN in your HTML page −

      

Hello

Output

This will produce the following result —

Hello, World! We are using jQuery from jQuery CDN.

Источник

jQuery is a JavaScript framework that runs very fast with a very small size of JavaScript library code. It can easily implement a lot of JavaScript animation effects by operating DOM objects, processing events, and handling Ajax interactions. jQuery can simplify your JavaScript source code to improve your development speed and JavaScript code execution performance. In this article, I will tell you how to add jQuery links in both Html files and javascript files, and how to use jQuery to implement some basic animations with examples.

Читайте также:  Jquery carousel with html
  1. The first thing you need to do is to download the jQuery library js file and add it into your Html file.
  2. Open a web browser and go to the jQuery library js file download page http://jquery.com/download/. Find the jQuery library js file download link, right-click it, in the popup menu list click Save Link As menu.
  3. The above action will save the jQuery library js file in a local folder, you can save it to any folder( for example your project lib folder).
  4. Next, you need to add the jQuery link in your Html page file like below source code. Below source code should be added in the head section of the target Html file.
  1. If you add the jQuery link in the Html source code, you must add the below code in every Html file head section. The disadvantage of this method is that if you want to use a new jQuery version you must update all Html files one by one manually, this is not a good method.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/OvyqoKL97Y0

2.1 Use document.writeln() Function To Add jQuery Link In JavaScript File.

  1. Add below js code in the include-jquery.js file, and then import the include-jquery.js file in test.html.
  2. include-jquery.js
// invoke javascript document object's writeln function to write 'import jquery lib js file string' in html page source code. document.writeln('');
/* Use $ to use jQuery object when the document load ready, then execute the anonymous funcion() */ $(document).ready(function() < // Register click event to all html a tag, the callback function will be triggered when user click a link in the html page. $('a').click(function(event) < // Prevent the link default behaviour, then click it will not open a target url. event.preventDefault(); // Popup an alert dialog. alert('The link will fade out.') // Fade out the link in the html page. $(this).hide("slow"); >); >);

2.2 Add jQuery By Create Script Element In Another JavaScript File.

// When the window load process complete then invoke load jquery lib js file function. window.load = load_jquery_lib_js_file(); function load_jquery_lib_js_file() < // Create a html script element. var script = document.createElement('script'); // Set the html script element's type to 'text/javascript'. script.type = 'text/javascript'; // Set the html script element's src attribute value to google online jquery lib js file. script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'; // When the jquery script load complete then invoke the handler function for different browsers. script.onreadystatechange = handler; script.onload = handler; // Append the html script element to the end of html page head section. document.getElementsByTagName('head')[0].appendChild(script); >// This function will be executed after jquery lib file has been loaded successfully. function handler()< // Write an message in browser console. console.log('jquery added :)'); // Create a html script element. var script = document.createElement('script'); // Set the html script element's type to 'text/javascript'. script.type = 'text/javascript'; // Set the html script element's src attribute value to the js file which will use jQuery object( it is use-jquery-when-jquery-is-loaded.js in this example ). script.src = './use-jquery-when-jquery-is-loaded.js'; // Append the html script element to the end of html page head section. document.getElementsByTagName('head')[0].appendChild(script); >
/* Use $ to use jQuery object when the document load ready, then execute the anonymous funcion() */ $(document).ready(function() < // Register click event to all html a tag, the callback function will be triggered when user click a link in the html page. $('a').click(function(event) < // Prevent the link default behaviour, then click it will not open a target url. event.preventDefault(); // Popup an alert dialog. alert('The link will fade out.') // Fade out the link in the html page. $(this).hide("slow"); >); >);

3. jQuery Example.

  1. This example contains two html files, hellow-world-alert.html and hello-world-add-number.html.
Читайте также:  Python install libs from requirements

3.1 jQuery Hello World Alert Example.

  1. In the first example, it will display an alert dialog when the page is loaded.
  2. There is also a button with text “Welcome to JQuery world” in the page, when the button is clicked, it will show two alert dialog, the first alert dialog’s text is ‘You click the button’, the second alert dialog’s text is the button original text, then it will set button text to ‘Wish you like JQuery 🙂‘ and change the button’s text and background color.
  3. hello-world-alert.html
         

3.2 jQuery Hello World Add Number Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/u-x9_IQssaU

  1. This example uses jQuery to set the Html p tag css style. It also bind the mouse move event of the div tag, when your mouse moves in the div tag, the number will increase.
  2. hello-world-add-number.html
     -->    

Hello World From JQuery!

4.1 Question.

  1. I get some jQuery code from the internet, and it meets my requirements, so I want to add those jQuery codes to my Html page. I tried some methods, but failed, can anyone give me some help? Thanks a lot.

4.2 Answer1.

  1. You can create a new javascript file such as common-function.js and then paste your jQuery code into it.
  2. You can save this file in your js project home-directory/js folder. Then you can follow this article to add both the jQuery library js file and the common-function.js file into your Html page ( refer to sections 1, 2 at the beginning of this article ).
  3. And then you can use the jQuery code that you get from the internet. But you also need to make sure the web element id or name in the jQuery code matches the web element in your Html web page.

5. How To Use jQuery To Include Html File Content.

5.1 Question.

  1. My Html page (index.html) contains several other Html pages using the tag like below.

5.2 Answer1.

  1. You can use jQuery’s load() method to load the Html page content (menu.html, detail.html) into the DOM tree of the index.html page.
  2. Then the jQuery object that is created in the index.html page can access and manipulate the content of the menu.html, detail.html.
  3. Below is the example source code.

Источник

Оцените статью