有人知道这个吗?
我想使用变量名调用一个函数。
编辑:
我在这里发布了一个fiddle,展示了我正在尝试做的事情:
http://jsfiddle.net/sAzPA/
<div id="some">
...
</div>
js:
(function($){
$.fn.MyPlugin = function(){
return this.each(function(){
var somefunction = function(arg1, arg2){ alert(arg1); },
someotherfunction = function(arg1, arg2){ alert(arg2); },
reallyimportantfunction = function(arg1, arg2){
var foo = $(this).attr('id') + 'function';
// here I want to call the function with the foo value as name, and pass it arg1 and arg2
$()[foo](arg1, arg2); // <- doesn't work
};
reallyimportantfunction();
});
};
})(jQuery);
jQuery(function($){
$('#some').MyPlugin ();
});
JQuery['functionName']();
。 - SpudleyJQuery()['functionName']()
。但是如果你正常调用该函数,你通常会怎么称呼它?任何时候你可以使用.functionName()
,都可以将其替换为['functionName']()
,因此请查看您当前使用该函数的情况,并根据需要调整这种技术。 - Spudleywindow['functionName']()
示例应该可以正常工作 -- 如下所示:window['somefunction']('bla', 'bla');
- Spudley