在JQuery中是否可以跳过可选参数并传递下一个参数?如果是,jquery函数如何确定哪个值是哪个参数?
一个例子:
//
// function signature is
// jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
//
// function is called as
//
$.get('ajax/test.html', function (data) {
$('.result').html(data);
alert('Load was performed.');
});
Run Code Online (Sandbox Code Playgroud)
在url之后调用$ .get()跳过[,data],然后传递成功回调.
是的,甚至可以从中间跳过可选参数.jQuery查看参数的类型以确定哪个是哪个.看看签名jQuery.load:
.load(url [, data] [, complete(responseText, textStatus, XMLHttpRequest)])
Run Code Online (Sandbox Code Playgroud)
以下工作如预期:
.load("test.php")
.load("test.php", {foo: bar})
.load("test.php", {foo: bar}, function(){})
.load("test.php", function(){}) // you skipped the 2nd parameter? no.
Run Code Online (Sandbox Code Playgroud)
你会注意到,这三个参数是:string,object和function.在函数内部,jQuery可以轻松检查传递的参数数量及其类型.如果它注意到第二个参数是一个函数,它将假定data参数被跳过并相应地采取行动.
令您惊讶的是,还有另一个jQuery.load具有不同签名的功能,它完全不同:
.load(handler(eventObject))
.load([eventData], handler(eventObject))
Run Code Online (Sandbox Code Playgroud)
同样,jQuery可以通过查看参数来确定要触发的方法.
| 归档时间: |
|
| 查看次数: |
2692 次 |
| 最近记录: |