jquery loads files via Ajax in Load ()

Source: Internet
Author: User

The full format of the Load method is: Load (URL, [data], [callback]),

which

URL: Refers to the address of the file to be imported.
data: Optional parameters; Because load can not only import static HTML files, but also import dynamic scripts, such as PHP files, so to import a dynamic file, we can put the parameters to pass here.
callback: optional parameter; is another function that is executed after the Load method is invoked and the server responds.

Directly on the code:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
 <title > New Document </title>
 <meta name= "generator" content= "EditPlus"
 <meta name= " Author "content=" "
 <meta name=" Keywords "content=" ""
 <meta name= "Description" Content= ""
<body>
 <script src= "Jquery.js" ></SCRIPT>
&NBSP;<SCRIPT>
$ (document). Ready (function () {
   $ (' #but '). Click (function () {
        $ (' #test '). Load ("111.txt");
  });
});
 </script>
 <button id= "But" > change text </button>
 <div id= "test" >< H2> changes the text </body>

Own local to have jquery.js and 111.txt text Document Oh, this text document should have content!


filter the loaded HTML document

After the URL Riga of the load, you can follow the selector.

For example:

The code is as follows Copy Code

$ ("body"). Load ("test.html #a");

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:

The code is as follows Copy Code
$ ("#resText"). Load ("test.html. para");


Delivery 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.

The code is as follows Copy Code

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" () {
//......
});

How to use callback

For example, we can use the callback function when the load method gets the server response and slowly displays the loaded content. The code is as follows:

  code is as follows copy code

$ ("#go"). Click (function () {

  $ (#myID). Load ("welcome.php", {"lname": "Cai", "fname": "Adam", function () {

& nbsp;   $ ("#myID"). FadeIn (' slow ');}

 );

});

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.