我想绑定(bind)到带有多个参数的元素函数。怎样做才是对的?
例如我有函数:
function test(arg1, arg2, arg3) {
console.log("arg1:", arg1);
console.log("arg2:", arg2);
console.log("arg3:", arg3);
}
我想将其绑定(bind)到 ID 为 #btn
的元素,但所有参数都在数组中:
var array = ['Hello', ' my', ' world!'];
我该怎么做?我会尝试这样的事情:
$("#btn").bind("click", array, test);
有什么建议吗?
最佳答案
你可以这样做:
var array = ['Hello', ' my', ' world!'];
$("#btn").bind("click", function() {
test.apply(null, array);
});
或者使用$.proxy
方法添加参数:
$("#btn").bind("click", $.proxy.apply($.proxy, [test, null].concat(array)));
关于javascript - 绑定(bind)多个参数的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135791/