元素,事件,ui变量是什么意思?—jQuery (UI)



例如:(http://api.jqueryui.com/sortable/)

$( ".selector" ).sortable({
    deactivate: function( event, ui ) {}
});
很多jQuery API的函数描述都有这类参数。它们有多种格式:el、element、elem、ev、event、ui等。谁能给我解释一下这个怎么解释?我测试了上面的函数,它似乎不需要任何输入?

它们是参数。你可以随意命名它们:

function myFunc(a){
    alert(a);
}
myFunc("Hello");

等于:

function myFunc(whatever){
    alert(whatever);
}
myFunc("Hello");

另外,因为javascript不是强类型的,所以不需要调用具有正确参数数量的函数:

function fourArgs(a, b, c, d){
}
function oneArg(a){
}
fourArg("Hello"); // a will be the string 'Hello', b c and d will be undefined
oneArg("Hello", 1, 2, 3); // a will be the string 'Hello', the other values are not referenced (they can still be accessed through arguments)

所以,你创建了一个函数并把它传递给jQuery作为回调。jQuery将始终使用文档中指定的参数调用它。是否要在您创建的函数中命名要使用的参数取决于您。如果您不打算使用它们,可以省略它。

最新更新