返回值:jQueryload(handler(eventObject))

Bind an event handler to the "load" JavaScript event.

This method is a shortcut for .bind('load', handler).

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

It is possible that the load event will not be triggered if the image is loaded from the browser cache. To account for this possibility, we can use a special load event that fires immediately if the image is ready. event.special.load is currently available as a plugin.

For example, consider a page with a simple image:

<img src="book.png" alt="Book" id="book" />

The event handler can be bound to the image:

$('#book').load(function() {
  // Handler for .load() called.
});

As soon as the image has been loaded, the handler is called.

In general, it is not necessary to wait for all images to be fully loaded. If code can be executed earlier, it is usually best to place it in a handler sent to the .ready() method.

The Ajax module also has a method named .load(). Which one is fired depends on the set of arguments passed.

示例:

Run a function when the page is fully loaded including graphics.

jQuery 代码:
$(window).load(function () {
  // run code
});

示例:

Add the class bigImg to all images with height greater then 100 upon each image load.

jQuery 代码:
$('img.userIcon').load(function(){
  if($(this).height() > 100) {
    $(this).addClass('bigImg');
  }
});

返回值:jQueryload(url, [data], [complete(responseText, textStatus, XMLHttpRequest)])

Load data from the server and place the returned HTML into the matched element.

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data. This means that most uses of the method can be quite simple:

$('#result').load('ajax/test.html');

The provided callback, if any, is executed after this post-processing has been performed:

$('#result').load('ajax/test.html', function() {
  alert('Load was performed.');
});

In the two examples above, if the current document does not contain an element with an ID of "result," the .load() method is not executed.

The POST method is used if data is provided as an object; otherwise, GET is assumed.

Note: The event handling suite also has a method named .load(). Which one is fired depends on the set of arguments passed.

Loading Page Fragments

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

We could modify the example above to use only part of the document that is fetched:

$('#result').load('ajax/test.html #container');

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

示例:

Load the main page's footer navigation into an ordered list.

<!DOCTYPE html>
<html>
<head>
<style>
 body{ font-size: 12px; font-family: Arial; }
 </style>
<script src="jquery.min.js"></script>
</head>
<body>


<b>Footer navigation:</b>
<ol id="new-nav"></ol>


<script>


  $("#new-nav").load("/ #jq-footerNavigation li");


</script>
</body>
</html>
演示:

示例:

Display a notice if the Ajax request encounters an error.

<!DOCTYPE html>
<html>
<head>
<style>
  body{ font-size: 12px; font-family: Arial; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>


<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
  

<script>


$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});
  

</script>
</body>
</html>
演示:

示例:

Load the feeds.html file into the div with the ID of feeds.

jQuery 代码:
$("#feeds").load("feeds.html");
结果:
<div id="feeds"><b>45</b> feeds found.</div>

示例:

pass arrays of data to the server.

jQuery 代码:
$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );

示例:

Same as above, but will POST the additional parameters to the server and a callback that is executed when the server is finished responding.

jQuery 代码:
$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});