如果您在加载 JQuery 之后加载此代码,您将能够使用超时参数调用 .load()。
jQuery.fn.load = function( url, params, callback, timeout ) {
if ( typeof url !== "string" ) {
return _load.call( this, url );
// Don't do a request if no elements are being requested
} else if ( !this.length ) {
return this;
}
var off = url.indexOf(" ");
if ( off >= 0 ) {
var selector = url.slice(off, url.length);
url = url.slice(0, off);
}
// Default to a GET request
var type = "GET";
// If the second parameter was provided
if ( params ) {
// If it's a function
if ( jQuery.isFunction( params ) ) {
if( callback && typeof callback === "number"){
timeout = callback;
callback = params;
params = null;
}else{
// We assume that it's the callback
callback = params;
params = null;
timeout = 0;
}
// Otherwise, build a param string
} else if( typeof params === "number"){
timeout = params;
callback = null;
params = null;
}else if ( typeof params === "object" ) {
params = jQuery.param( params, jQuery.ajaxSettings.traditional );
type = "POST";
if( callback && typeof callback === "number"){
timeout = callback;
callback = null;
}else if(! timeout){
timeout = 0;
}
}
}
var self = this;
// Request the remote document
jQuery.ajax({
url: url,
type: type,
dataType: "html",
data: params,
timeout: timeout,
complete: function( res, status ) {
// If successful, inject the HTML into all the matched elements
if ( status === "success" || status === "notmodified" ) {
// See if a selector was specified
self.html( selector ?
// Create a dummy div to hold the results
jQuery("<div />")
// inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE
.append(res.responseText.replace(rscript, ""))
// Locate the specified elements
.find(selector) :
// If not, just inject the full result
res.responseText );
}
if ( callback ) {
self.each( callback, [res.responseText, status, res] );
}
}
});
return this;
};
不确定您是否有兴趣覆盖任何“标准”JQuery 函数,但这将允许您按照您描述的方式使用 .load() 。