JQuery Ajax's Load () method _jquery

Source: Internet
Author: User
Tags blank page

The load () method is the simplest and most commonly used Ajax method in jquery to load remote HTML code and insert it into the DOM. Its grammatical structure is:

  Load (URL [, data][, Callback])

The load () method parameter is explained in the following table:

Parameter name Type Description
Url String Request URL address of HTML page
Data (optional) Object Key/value data sent to the server
Callback (optional) Function callback function when the request completes, whether the request succeeds or fails

1. Load HTML Document
First create an HTML file called test.html to prepare for background Ajax loading. The code is as follows:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>

<div class= "comment" >
Comments have been made:
</div>
<div class= "comment" >
<p class= "para" > sofa. </p>
</div>
<div class= "comment" >
<p class= "Para" > Bench. </p>
</div>
<div class= "comment" >
<p class= "Para" > Flooring. </p>
</div>
</body>

Then create a new blank page, add two elements above the:<button> button to trigger the Ajax event, and the element with the id "ResText" is used to display the appended HTML content. The next step is to write the jquery code. When the DOM element is loaded, call the Laod () method by clicking the button with the id "send", and then load the contents of the test.html into an element with the id "ResText". So the code is as follows:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<script type= "Text/javascript" src= "/jquery-1.3.2.js" ></script>
<body>

<input type= "button" id= "Send" value= "ajax Fetch"/>
<div id= "ResText" ></div>

<script type= "Text/javascript" ><!--
$ (document). Ready (function () {
$ ("#send"). Click (function () {
$ ("#resText"). Load ("test.html");
});
});
--></script>

</body>

When the button is clicked, an interface with the following image appears:

  
Obviously, the load () method completes the original tedious work. Developers only need to use the jquery selector to specify the target location for the HTML fragment, and then pass the URL of the file to be loaded as a parameter to the load () method.
2, filter the loaded HTML document
The last example is to load the contents of a test.html page into an element with an ID of "ResText". If you only need to load certain elements within the test.html page, you can use the URL parameters of the load () method to achieve the goal. By specifying a selector for the URL parameter, you can easily filter the content you want from the HTML document that is loaded.
The syntax structure of the URL parameter for the load () method is: "URL selector." Notice that there is a space between the URL and the selector.
For example, you only need to load content with class "Para" in the test.html page, which you can do using the following code:
$ ("#resText"). Load ("test.html. para");
The results of the operation are shown below:


3. Transfer Mode
The load () method is passed from the parameter data to a dynamic designation. If there is no parameter passing, the Get method is passed, and conversely, automatically converted to post.

Copy Code code as follows:

No parameter pass, then the Get method
$ ("#resText"). Load ("test.php", function () {
//......
});
With parameter passing, it is the Post method
$ ("#resText"). Load ("test.php", {name: "xht555", Age: "},function" () {
//......
});

4. Callback Parameters
For operations that must be completed before the load is complete, the load () method provides a callback function (callback) that has three parameters, representing the content requested, the request status, and the XMLHttpRequest object, respectively, with the jquery code as follows:
Copy Code code as follows:

$ ("#resText"). Load ("test.html", function (responsetext,textstatus,xmlhttprequest) {
ResponseText: Request returned content
Textstatus: Request Status: Success, error, notmodified, timeout these 4 kinds
Xmlhttprequest:xmlhttprequest objects
});

Note: In the load () method, the callback function (callback) is triggered whenever the request completes (complete), regardless of whether the Ajax request succeeds.

Related Article

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.