Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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 - Fatal编程技术网

Javascript—如何在运行时使用变量替换现有代码

Javascript—如何在运行时使用变量替换现有代码,javascript,Javascript,我有下面的代码,我想根据变量值在运行时替换代码,如何使用javascript实现这一点。此外,请忽略科尼移动使用的格式、动画等 //desired out put inside the run time code. FormA.ItemFlexContainer0.animate //index has a value of 0 for now. var index = 0; var ItemFlexContainerString = "ItemFlexCo

我有下面的代码,我想根据变量值在运行时替换代码,如何使用javascript实现这一点。此外,请忽略科尼移动使用的格式、动画等

//desired out put inside the run time code. 
    FormA.ItemFlexContainer0.animate

    //index has a value of 0 for now. 
    var index = 0;
    var ItemFlexContainerString = "ItemFlexContainer"+index;
    FormA.ItemFlexContainerString.animate

    Error :- Type Error null is not an object 

由于ItemFlexContainerString不存在,它将抛出错误

您需要括号表示法:

FormA[ItemFlexContainerString]animate

在我的例子中,这是有效的,而现有问题中提到的答案是无效的,FormCart[“ItemFlexContainer”+index].animate,数组应该有双引号,并以书面形式附加索引。