调用一个名为变量的jQuery函数 [英] calling a jQuery function named in a variable

查看:98
本文介绍了调用一个名为变量的jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个jQuery函数,像
函数setOne();
setTwo(); setThree();



和一个变量 var number ,two,three。

当数值为one时,如何调用函数setOne(),当数值为two时函数setTwo 等等...?

非常感谢你提前。任何帮助都会有所帮助。

解决方案

如果你的函数在全局范围内(在窗口对象上),你可以这样做:
$ b

  //根据编号调用函数setOne,setTwo,...。 
window [set+ number]();

使用 eval 可让您运行函数在本地范围内:

  eval(set+ number +()); 

什么时候JavaScript的 eval() not 邪恶?


I have several jQuery function like function setOne(); setTwo(); setThree();

and a variable var number that values respectively "one", "two", "three".

How can I call function "setOne()" when number values "one", function "setTwo" when number values "two" and so on...?

Thank you so much in advance. Any help will be apreciated.

解决方案

If you have your function in the global scope (on the window object) you can do:

// calls function setOne, setTwo, ... depending on number.
window["set" + number](); 

And using eval will allow you to run functions in local scope:

eval("set" + number + "()");

When is JavaScript's eval() not evil?

这篇关于调用一个名为变量的jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆