Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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_Jquery_Variables_Dynamic_Dynamic Variables - Fatal编程技术网

JavaScript动态变量名

JavaScript动态变量名,javascript,jquery,variables,dynamic,dynamic-variables,Javascript,Jquery,Variables,Dynamic,Dynamic Variables,好的,我想在用户点击时创建变量,每次点击都会添加一个新变量。我目前正在使用jquery和javascript,但我无法在服务器端完成这项工作,必须在浏览器中完成 newCount = document.getElementById('hello').innerHTML; $('.hello').click(function(){ //set count fast enumeration newCount++; var hello + new

好的,我想在用户点击时创建变量,每次点击都会添加一个新变量。我目前正在使用jquery和javascript,但我无法在服务器端完成这项工作,必须在浏览器中完成

newCount = document.getElementById('hello').innerHTML;
    $('.hello').click(function(){
        //set count fast enumeration
        newCount++;
        var hello + newCount = '<p>Hello World</p>';
    }); 
newCount=document.getElementById('hello').innerHTML;
$('.hello')。单击(函数(){
//集计数快速枚举
newCount++;
var hello+newCount='hello World

'; });

所以我希望变量是hello1、hello2、hello3、hello4等等。

您可以使用
窗口

var window['hello' + newCount ] = '<p>Hello World</p>';
var窗口['hello'+newCount]='hello World

';
同样地

newCount = document.getElementById('hello').innerHTML;
    $('.hello').click(function(){
        //set count fast enumeration
        newCount++;
        var window['hello' + newCount ] = '<p>Hello World</p>';
        alert(window['hello' + newCount ]);
    }); 
newCount=document.getElementById('hello').innerHTML;
$('.hello')。单击(函数(){
//集计数快速枚举
newCount++;
变量窗口['hello'+newCount]='hello World

'; 警报(窗口['hello'+newCount]); });
您只能使用括号表示法,这意味着您必须将变量附加到某物上。
全局范围应该是window,因此应该是
window['hello'+newCount]
,但是用一堆随机属性污染全局名称空间听起来不是个好主意,因此使用对象似乎更好

var vars = {};
var newCount = parseInt($('#hello').html(), 10);

$('.hello').click(function(){
    newCount++;
    vars['hello' + newCount] = '<p>Hello World</p>';
}); 

alert( vars['hello1'] );
var-vars={};
var newCount=parseInt($('#hello').html(),10);
$('.hello')。单击(函数(){
newCount++;
vars['hello'+newCount]='hello World

'; }); 警报(vars['hello1']);
在JavaScript中(我知道),有两种方法可以创建动态变量:

eval Function
window object
eval:

var pageNumber = 1;
eval("var text" + pageNumber + "=123;");
alert(text1);
window object:

var pageNumber = 1;
window["text" + pageNumber] = 123;
alert(window["text" + pageNumber]);
更多信息
我只需要使用一个简单的数组来存储变量。那么索引为n的对象将是变量n等等

var newCount = 0;
$('#btn').click(function(){

    newCount++;
    $('#hello').html('hello' + newCount);
}); 
最简单的方法

var types = {};

for(var i=1; i<=3; i++){
types["ashish"+i] = 'The value of dynamic variable, val';
}
var-types={};

对于(var i=1;iwhy-1)我的答案?这也将是正确的。这没有错。这是错误的。当你获得否决票时,也许你应该花时间测试你的代码。不要使用“动态变量名”。使用适当的映射(普通对象)或序列(数组)。