开发者

How use dynamic variable without missing ] after element list?

I have a dynamic variable like this:

var oneButton= null;
var oneMoreButton= null;
var differentNameButton= null;
var anotherButtonDifferent= null;

Then i have this:

switch(valor){
    case 0:data = "oneButton"; break;
    case 1:data = "oneMoreButton";break;
    case 2:data = "differentNameButton";break;
    case 3:data = "anotherButtonDifferent";break;
}

Then finally i have this:

switch(anotherValor){           
case 1:eval(data + ' = ' + document.getElementById('oneNameDiv').parentNode);break;
case 2:eval(data + ' = ' + document.getElementById('anotherDivName').parentNode);break;
case 3:eval(data + ' = ' + document.getElementById('oneMoreDivWithDifferentName').parentNode);break;
case 4:eval(data + ' = ' + document.getElementById('hereIsAnotherDivName').parentNode);break;
}

After i try to execute it i get the error in firefox console:

missing ] after element list
http://localhost:8090/myapplication/functions.js 
test1 = [objectHTMLDivElement]

Someone开发者_开发知识库 knows what's happening?


I dont know what exactly you are trying to achieve, but your code could be better. Something like this for example:

var data = document.getElementById('div'+valor).parentNode;

Assuming there is a variable called valor with the number of your div.

Could you explain what you are doing?


You can declare an array like this:

var test = [];

Then you can use "valor" directly:

test[valor]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜