调用变量中命名的jQuery函数

时间:2022-06-25 20:25:21

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

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

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

和一个变量var,分别表示“一”,“二”,“三”。

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

当数值为“1”时,如何调用函数“setOne()”,当数字值为“2”时,如何调用“setTwo”等等?

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

非常感谢你提前。任何帮助都会被贬低。

6 个解决方案

#1


34  

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将允许您在本地范围内运行函数:

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

When is JavaScript's eval() not evil?

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

#2


8  

Create a name -> function map:

创建名称 - >功能图:

var funcs = {
    'one': setOne,
    'two': setTwo
    /*...*/
};

Then you call the function with:

然后用以下方法调用该函数:

funcs[number]();

#3


6  

If the variable details the actual name of the JQuery function and you want to apply the function to a DOM element like 'body', you can do the following:

如果变量详细说明了JQuery函数的实际名称,并且您希望将该函数应用于DOM元素(如“body”),则可以执行以下操作:

 $('body')['function-name']('params');

#4


2  

Provided your functions are in the global scope, try:

如果您的功能在全球范围内,请尝试:

function setOne() {
  console.log('setOne called');
}
function setTwo() {
  console.log('setTwo called');
}
function setThree() {
  console.log('setThree called');
}

var number, funcName;

number = 'one';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setOne called

number = 'two';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setTwo called

number = 'three';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setThree called

#5


1  

As simple as this is:

这很简单:

function hello(){
    alert("hello");
}
var str = "hello";
eval(str+"()");

#6


0  

Why do you have three functions for that?

为什么你有三个功能呢?

var number;
function setNumber(n) {
    number = n;
}

setNumber(1) will set number to 1

setNumber(1)将number设置为1

setNumber(2) will set number to 2

setNumber(2)将number设置为2

ect

ECT

#1


34  

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将允许您在本地范围内运行函数:

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

When is JavaScript's eval() not evil?

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

#2


8  

Create a name -> function map:

创建名称 - >功能图:

var funcs = {
    'one': setOne,
    'two': setTwo
    /*...*/
};

Then you call the function with:

然后用以下方法调用该函数:

funcs[number]();

#3


6  

If the variable details the actual name of the JQuery function and you want to apply the function to a DOM element like 'body', you can do the following:

如果变量详细说明了JQuery函数的实际名称,并且您希望将该函数应用于DOM元素(如“body”),则可以执行以下操作:

 $('body')['function-name']('params');

#4


2  

Provided your functions are in the global scope, try:

如果您的功能在全球范围内,请尝试:

function setOne() {
  console.log('setOne called');
}
function setTwo() {
  console.log('setTwo called');
}
function setThree() {
  console.log('setThree called');
}

var number, funcName;

number = 'one';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setOne called

number = 'two';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setTwo called

number = 'three';
funcName = 'set' + number.charAt(0).toUpperCase() + number.slice(1);
window[funcName](); // output: setThree called

#5


1  

As simple as this is:

这很简单:

function hello(){
    alert("hello");
}
var str = "hello";
eval(str+"()");

#6


0  

Why do you have three functions for that?

为什么你有三个功能呢?

var number;
function setNumber(n) {
    number = n;
}

setNumber(1) will set number to 1

setNumber(1)将number设置为1

setNumber(2) will set number to 2

setNumber(2)将number设置为2

ect

ECT