How jQuery works

Source: Internet
Author: User

Recently, I am working on a project and need to use ajax. I have been working on ajax projects for many years and jQuery is not mature yet. I need to write a lot of Javascript code myself.

Recently I found jQuery very mature. I have read the official document of jQuery. Here we will first provide the content of the Getting Started chapter.

Topic: jQuery from entry to entry

A Brief Introduction to jQuery and some concepts about how to use jQuery

This section mainly teaches you how to start using jQuery. If you do not have a test page, create an HTML page from the following code.

 
 
  1. <!doctype html>  
  2.  
  3.     <meta charset="utf-8"> 
  4.     <title>Demo</title> 
  5.  
  6.  <body> 
  7.     <a href="http://jquery.com/">jQuery</a> 
  8.     <script src="jquery.js"></script>
  9.     <script> 
  10.     </script>
  11.  </body> 

Edit the src attribute of the Script tag to point to your jQuery. js attribute. For example, if your jQuery. js and HTML files are in the same folder:

 
 
  1. <script src="jquery.js"></script> 

Run the code when the page is loaded.

The first thing many Javascript programmers have to do is add such code to their programs:

 
 
  1. window.onload = function(){ alert("welcome"); } 

This code will be correctly executed when the page is loaded successfully. However, the problem with this Code is that the Javascript code will be executed after all the image information is downloaded (the advertisement bar is included here ). The first reason for using Window. onload is that the HTML document is not fully loaded when you first run this code.

To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event:

JQuery has a very simple declaration to check the document and determine whether the document is ready. This is called ready event:

 
 
  1. $(document).ready(function(){  
  2.    // Your code here  
  3.  }); 

In ready event, add a hyperlink click handling method.

 
 
  1. $(document).ready(function(){  
  2.   $("a").click(function(event){  
  3.     alert("Thanks for visiting!");  
  4.   });  
  5. }); 

Save the HTML file, refresh the file in the browser, and click the hyperlink in the webpage. A prompt box is displayed before you connect to the specified page.

In click and many other events, you can use event. preventDefault () here to prevent it from performing default operations.

 
 
  1. $(document).ready(function(){  
  2.    $("a").click(function(event){  
  3.      alert("As you can see, the link no longer took you to jquery.com");  
  4.      event.preventDefault();  
  5.    });  
  6.  }); 

Complete example

Here is a complete example of an HTML file to demonstrate the content we described earlier. Note that Google's CDN is connected to load the core library of jQuery. You 'd better put it in a separate file and load it in the Head tag.

 
 
  1. <!DOCTYPE html> 
  2.  
  3.  
  4.    <meta charset="utf-8"> 
  5.    <title>jQuery demo</title> 
  6.  
  7.  <body> 
  8.    <a href="http://jquery.com/">jQuery</a> 
  9.    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
  10.    <script> 
  11.      $(document).ready(function(){  
  12.        $("a").click(function(event){  
  13.          alert("As you can see, the link no longer took you to jquery.com");  
  14.          event.preventDefault();  
  15.        });  
  16.      });  
  17.    </script> 
  18.  </body> 
  19.  

Add and delete css styles

Important: The following jQuery example should be placed in ready event, so that it can be executed immediately after the preparation of the document is complete.

Another common thing we do is to add (or delete) a css style.

First, we add some styles in the

 
 
  1. <style>  
  2.     a.test { font-weight: bold; }  
  3.  </style> 

Use the addClass method in the code.

 
 
  1. $("a").addClass("test"); 

All hyperlinks are bold.

To delete this style, you only need to use the remove class method. (Multiple styles can be added to html)

 
 
  1. $("a").removeClass("test"); 

Special effects

JQuery provides a set of special effects that can make your website stand out. You only need to add the following code to the test file to perform the test:

 
 
  1. $("a").click(function(event){  
  2.    event.preventDefault();  
  3.    $(this).hide("slow");  
  4.  }); 

If you click a hyperlink, the hyperlink you click will be hidden.

Callback Function

A callback function is a function that passes a parameter after the parent method runs successfully and is called. A callback function can run a parent function before it runs. Another important point is to know how to pass the callback correctly. This is also the correct syntax that I often forget.

Callback with no parameters

You can use the following code to set callback parameters.

 
 
  1. $.get('myhtmlpage.html', myCallBack); 

Note:The second parameter only contains the name of the callback function (not a string and does not contain parentheses). Javascript functions can be passed like parameters and executed in subsequent code.

Callback with Parameters

You may ask, "What would you do if the parameter is included? ".

Incorrect Method

Incorrect call method (not executed)

 
 
  1. $.get('myhtmlpage.html', myCallBack(param1, param2)); 

This method will not be called because it calls

 
 
  1. myCallBack(param1, param2) 

The execution result is passed to the $ get () function as the second parameter.

The correct method

The problem with the above method is that myCallBack is evaluated before being passed as a function. Javascrip and jQuery extend functions as function pointers, for example, IE's set Timeout function.

The following method creates an anonymous function and registers the callback function. Note that the function () {} anonymous function is used only for one thing. Two Parameters param1 and param2 are used to call the myCallBack method.

 
 
  1. $.get('myhtmlpage.html', function(){  
  2.  myCallBack(param1, param2);  
  3. }); 

Param1 and param2 are the parameters provided after the page is obtained by the $ get () method.

Original article: http://www.cnblogs.com/daitou0322/archive/2011/08/07/2130138.html

Edit recommendations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.