Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何连接字符串和变量以在javascript中创建新变量?_Javascript_String_Variables_For Loop_Addeventlistener - Fatal编程技术网

如何连接字符串和变量以在javascript中创建新变量?

如何连接字符串和变量以在javascript中创建新变量?,javascript,string,variables,for-loop,addeventlistener,Javascript,String,Variables,For Loop,Addeventlistener,找到了几个php解决方案,但找不到任何javascript解决方案 基本上,我正在尝试为我页面上的30个按钮制作一组EventListener。目前是这样写的: problem1buttonEl.addEventListener("click", problem1); problem2buttonEl.addEventListener("click", problem2); problem3buttonEl.addEventListener("click", problem3); problem

找到了几个php解决方案,但找不到任何javascript解决方案

基本上,我正在尝试为我页面上的30个按钮制作一组EventListener。目前是这样写的:

problem1buttonEl.addEventListener("click", problem1);
problem2buttonEl.addEventListener("click", problem2);
problem3buttonEl.addEventListener("click", problem3);
problem4buttonEl.addEventListener("click", problem4);
现在我想做一个for循环,让它更干净,类似这样:

for (var problemIncrement = 1; problemIncrement <= 30; problemIncrement++) {
'problem' + problemIncrement + 'buttonEl'.addEventListener("click", 
'problem' + problemIncrement);
}

for(var problemIncrement=1;problemIncrement

function problem(n) {
    return function () {
        // do something with n
    }
}

for (var i = 1; i <= 30; i++) {
    getElementById('problem' + i + 'buttonEl').addEventListener("click", problem(i));
}
功能问题(n){
返回函数(){
//用n做点什么
}
}

对于(var i=1;i为什么不使用带有参数的闭包来表示类似按钮的标识符的编号

function problem(n) {
    return function () {
        // do something with n
    }
}

for (var i = 1; i <= 30; i++) {
    getElementById('problem' + i + 'buttonEl').addEventListener("click", problem(i));
}
功能问题(n){
返回函数(){
//用n做点什么
}
}
对于(var i=1;i
或者类似的事情


或者类似的东西!

您可以使用括号符号
[]
来获取这些变量,通常它们应该在
窗口中定义
对象,因此代码应该是这样的:

for (var problemIncrement = 1; problemIncrement <= 30; problemIncrement++) {
    window['problem' + problemIncrement + 'buttonEl'].addEventListener("click", 
    window['problem' + problemIncrement]);
}

对于(var problemIncrement=1;problemIncrement您可以使用括号表示法
[]
来获取这些变量,通常它们应该在
窗口中定义
对象,因此您的代码应该是这样的:

for (var problemIncrement = 1; problemIncrement <= 30; problemIncrement++) {
    window['problem' + problemIncrement + 'buttonEl'].addEventListener("click", 
    window['problem' + problemIncrement]);
}

用于(var problemIncrement=1;problemIncrement如何创建n个函数?因此,您需要一个包含所有答案的数组,然后将其拆分为一个字符串?如何创建n个函数?因此,您需要一个包含所有答案的数组,然后将其拆分为一个字符串?啊,这也是GetElemById部分的智能集成,谢谢!啊,这是一个智能的集成getElemtById部分的集成,谢谢!好的,先生,刚刚剪下143行代码,非常感谢!好的,先生,刚刚剪下143行代码,非常感谢!