gpt4 book ai didi

javascript - 在 javascript/jquery 中传递可选参数

转载 作者:行者123 更新时间:2023-11-29 18:30:52 29 4
gpt4 key购买 nike

JQuery .load() 定义如下:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );

datacomplete() 参数是可选的。正确的?但是,我们如何调用它:

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

jquery 如何忽略第二个参数并知道我们提供了第一个和第三个参数?如果 3d 参数不是函数怎么办?

最佳答案

参见 source code .

load: function( url, params, callback ) {

if (typeof url !== "string" && _load) {
// My comment: the function.apply ia an
// alternative way to call a javascript function,
// where the number of parameter in `arguments`
// couldn't be determined
// the `arguments` is a special javascript variable.
// It's an array containing the parameters within
// the context of a function
return _load.apply(this, arguments);

// 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 ) ) {
// We assume that it's the callback
callback = params;
params = undefined;
// Otherwise, build a param string
} else if ( typeof params === "object" ) {
params = jQuery.param( params, jQuery.ajaxSettings.traditional );
type = "POST";
}
}

.... other code

关于javascript - 在 javascript/jquery 中传递可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8500968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com