简体   繁体   English

使用for循环定义全局变量

[英]Using for loops to define global variables

I am defining some global arrays at the top of my JS file. 我在JS文件的顶部定义了一些全局数组。 There are for loops to populate the arrays. 有for循环可用于填充数组。 I have done very similar things that have worked but for some reason, this is having issues. 我所做的工作非常相似,但由于某种原因,这存在问题。 The first for loop runs through only one time and the code below that never gets ran. 第一个for循环仅运行一次,并且下面的代码永远不会运行。

var group = 'a';
var RoofVar = 1;
var TrimVar = 1;
var BBVar = 1;
var imageArrayGroup0 = [];
var floatingButtonArrayGroup0 = [];
var labelArrayGroup0 = [];
for (a = 1; a < 3; a++) {
    labelArrayGroup0[a] = document.createElement("Label");
    labelArrayGroup0[a].type = "Label";
    floatingButtonArrayGroup0[a] = document.getElementById('floatingButton0' + (100 + a));
    floatingButtonArrayGroup0[a].style.textAlign = "center";
    floatingButtonArrayGroup0[a].style.paddingTop = "5px";
    labelArrayGroup0[a] = document.getElementById('floatingButton0' + (100 + x));
}

var imageArray0 = [];
var floatingButtonArray0 = [];
var labelArray0 = [];
for (b = 1; b < 5; b++) {
    imageArray0[b] = document.createElement("img");
    imageArray0[b].type = "image";
    floatingButtonArray0[b] = document.getElementById('floatingButton0' + (b));
    floatingButtonArray0[b].style.paddingLeft = "15px";
    floatingButtonArray0[b].style.paddingTop = "5px";
    labelArray0[b] = document.getElementById('floatingButton0' + b);
}

var imageArray1 = [];
var floatingButtonArray1 = [];
var labelArray1 = [];
for (c = 1; c < 3; c++) {
    imageArray1[c] = document.createElement("img");
    imageArray1[c].type = "image";
    floatingButtonArray1[c] = document.getElementById('floatingButton1' + (c));
    floatingButtonArray1[c].style.paddingLeft = "15px";
    floatingButtonArray1[c].style.paddingTop = "5px";
    labelArray1[c] = document.getElementById('floatingButton1' + c);
}

Paul had my answer. 保罗得到了我的回答。 I forgot to switch out the "x" for an "a". 我忘了将“ x”换成“ a”。

You have other issues than just " (100 + x) instead of (100 + a) . 您还有其他问题,而不仅仅是(100 + x)而不是(100 + a)

First, arrays in JavaScript are "zero-based" but your loops start at 1. This means you're putting things in but leaving spot '0' undefined, which will probably break things later. 首先,JavaScript中的数组是“从零开始的”,但是循环从1开始。这意味着您要放入东西,但未定义点“ 0”,这以后可能会破坏东西。

Next. 下一个。 at the beginning of the loop you set labelArrayGroup0[a] to a new blank label, but you never do anything to put it on the page. 在循环开始时,将labelArrayGroup0[a]设置为新的空白标签,但是您从不做任何将其放置在页面上的操作。 At the end the loop, you overwrite labelArrayGroup0[a] to a floatingButton element you pull from the page. 在最后的循环中,您覆盖labelArrayGroup0[a]floatingButton您从网页拉元素。

In fact, you do this in every loop - the last line resets the value of the 1st line. 实际上,您可以在每个循环中执行此操作-最后一行重置第一行的值。 It's not clear what you're trying to accomplish. 目前尚不清楚您要完成什么。 Presumably you intend to append labelArrayGroup0[a] to the page instead? 大概您打算将labelArrayGroup0[a]附加到页面上?

Also, in the middle, you set floatingButtonArrayGroup0[a] = document.getElementById('floatingButton0' + (100 + a)); 另外,在中间,您设置了floatingButtonArrayGroup0[a] = document.getElementById('floatingButton0' + (100 + a)); which is the same element you grab in the last line of the loop... 这与您在循环的最后一行抓取的元素相同...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM