你可以就“载荷”活动设立一个手稿。
$( img.whatever )
.load(function() { /* do stuff */ })
.attr( src , newURL);
http://www.un.org
$( img.reloadable ).live( load , function() { $(this).show(); });
// ...
$( img#someId ).hide().attr( src , newURL);
edit — whoa, where did that year go? Well, it turns out that one problem with that "live" approach I typed in way back when is that the "load" event does not bubble. Now what you can do, however, is leverage the way that "Image" objects (as opposed to <img>
DOM elements) behave. Basically, the function that changes the URL can use an "Image" element as the place to keep the handler. The code that changes the actual "src" attribute of the real <img>
tag would then also update the "src" of the "Image" object instance. The browser will only really load the image once (assuming cache control is all cool), but the browser will still call the "onload" handler of the "Image":
(function() {
var imageObj = new Image();
imageObj.onload = function() {
// code to run when image loads from server
};
$( #hypotheticalButton ).click(function() {
$( #imgToUpdate ).attr( src , newURL);
imageObj.src = newURL;
});
})();