如何通过名称调用jQuery函数。例如:
var fn = 'hide',
myObj = $("#smth");
// here I want to hide myObj ( $("#smth").hide() )
// my variants were:
// fn.call(myObj) - doesn't work
// myObj.fn() - doesn't work (I've not expected, just tried =) )
答案 0 :(得分:5)
访问该函数,就像使用变量名访问myObj
的任何其他成员一样,然后只需调用它:
var fn = 'hide',
myObj = $("#smth");
(myObj[fn])();
答案 1 :(得分:2)
这样做:
var fn = 'hide',
myObj = $("#smth");
myObj[fn]();
干杯