[英]Passing a jquery method as a parameter into a function?
Is it possible to pass a jQuery method as a parameter into a function , for example: 是否可以将jQuery方法作为参数传递给函数,例如:
function set_action(a,b)
{
$(a).b;
}
$(function(){
$('#div_id1').click(function(){
set_action('#div_id2','hide()');
});
});
? ?
You can access any property of an object with a string using bracket notation: 您可以使用方括号表示法使用字符串访问对象的任何属性:
var foo = {
bar: 5
};
console.log(foo['bar']);
So you could do: 因此,您可以执行以下操作:
function set_action(a,b) {
$(a)[b]();
}
set_action('#div_id2','hide');
Note that this will throw an error if the object does not have a callable property with that name. 请注意,如果对象没有具有该名称的可调用属性,则将引发错误。
function set_action(a,b) {
$(a)[b]();
}
$('#hello').click(function(){
set_action('#bye','hide');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.