JavaScript创建变量并使用循环为它们分配值 [英] JavaScript creating variables and assigning them a value using a loop

查看:77
本文介绍了JavaScript创建变量并使用循环为它们分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含以下代码的脚本:

I have created a script that contains the following code:

var position = 1;
var backButton = document.getElementById("theBackButton");
var nextButton = document.getElementById("theNextButton");
var introPos = document.getElementById("introductionText");
var posOne = document.getElementById("positionOne");
var posTwo = document.getElementById("positionTwo");
var posThree = document.getElementById("positionThree");
var posFour = document.getElementById("positionFour");
var posFive = document.getElementById("positionFive");
var posSix = document.getElementById("positionSix");
var posSeven = document.getElementById("positionSeven");
var posEight = document.getElementById("positionEight");
var posNine = document.getElementById("positionNine");
var posTen = document.getElementById("positionTen");
var posEleven = document.getElementById("positionEleven");

每个位置包含不同的内容,我使用按钮在每个位置之间移动.该脚本可以正常工作,但是我想知道是否存在一种更有效的方式来生成这些变量并将它们分配给这些值?我必须做很多这种类型的工作,在尝试学习JavaScript时,我读到了一些东西,即如果您发现自己不得不一遍又一遍地编写相同类型的代码,那么您可能就没有这样做了.对."

Each position contains different content and I use buttons to move between each position. The script works just fine, but I am wondering if there is a more efficient way to generate these variables and assign them to these values? I have to do this type of work a lot, and in my attempt at learning JavaScript I read something to the effect of "If you find yourself having to write the same type of code over and over again, you're probably not doing it right."

我提前知道我将有多少个职位,我想我可以使用循环来做到这一点,但是我不确定.我希望得到的结果是一堆变量[posOne,posTwo,posThree等],其对应值是document.getElementById("positionOne");

I know ahead of time how many positions that I will have and I'm thinking that I could use a loop to do this, but I'm not sure. The results I hope to get is a bunch of variables [posOne, posTwo, posThree, etc] with the corresponding value of document.getElementById("positionOne");

谢谢.

推荐答案

不是将posOne手动编码为posEleven或您需要迭代的任何东西,而是将数字字符串更改为数字:

Instead of handcoding posOne to posEleven or whatever you need iterate but changing number strings to digits:

 var position = 1;
 var totalPositions=11;
 var backButton = document.getElementById("theBackButton");
 var nextButton = document.getElementById("theNextButton");
 var introPos = document.getElementById("introductionText");

 var totalPositions=11;
 var pos=[];

 for (i=1; i<=totalPositions; i++) {
        pos[i] = 'pos'+i+'= document.getElementById("position'+i+'")';
 }

 console.log(pos);

需要更改DOM元素ID,例如div ID或要操作的任何内容.

Need to change your DOM elements id, say div ids or whatever you are going to manipulate.

密切关注 console.log ,也许在您不再需要后就消除它.

Keep aqn eye on the console.log, maybe eliminate after you don't need anymore.

这篇关于JavaScript创建变量并使用循环为它们分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆