简体   繁体   English

具有元素id的Javascript局部变量

[英]Javascript local variable with element id

I'm having this problem in JS - which is not my native language at all. 我在JS中遇到这个问题 - 根本不是我的母语。 I have declared a global variable var divID = 0; 我已经声明了一个全局变量var divID = 0; which just is a unique number. 这只是一个独特的数字。 There is a button on my HTML page that, when you click it, will add a bunch of stuff to the DOM. 我的HTML页面上有一个按钮,当您单击它时,会向DOM添加一堆内容。

Something like: 就像是:

function addStuff() {
  divID++;
  newDiv = document.createElement("div");
  newDiv.id = "div" + divID;
  // Create a bunch more nodes under newDiv
  // including some button with id: "newButton" + divID
  document.getElementById("someDiv").appendChild(newDiv);
  $("#newButton" + divID).click(function() {
    // do stuff with child nodes of "newDiv" + divID
  });
}

I hope this makes sense. 我希望这是有道理的。 Basically, each time addStuff() is called, a block of stuff is added to the DOM. 基本上,每次调用addStuff()时,都会向DOM添加一块东西。 If the user clicks it repeatedly, the same, identical stuff is added over and over, one after another. 如果用户反复点击它,则一个接一个地反复添加相同的相同内容。

Each block of stuff that is added has some ID suffix that comes from divID , and certain elements in a block have events which affect other elements of the same block. 添加的每个块的内容都有一些来自divID ID后缀,并且块中的某些元素具有影响同一块的其他元素的事件。 Each block should be self-contained. 每个块应该是独立的。

However, when the user clicks the button twice, to add two blocks - one with ID suffix 1 and one with ID suffix 2, the events for both blocks affect the child elements for the most recently added block always. 但是,当用户单击按钮两次时,要添加两个块 - 一个具有ID后缀1,另一个具有ID后缀2,两个块的事件始终会影响最近添加的块的子元素。

So imagine I have 10 blocks of stuff. 所以想象我有10块东西。 All events for all blocks that should be self-contained actually affect the last block, not the block in which they reside. 应该是自包含的所有块的所有事件实际上影响最后一个块,而不是它们所在的块。

I know this sort of thing happens in C# when I spool up threads in a loop and the iterating variable plays some role in the thread body: 我知道这种事情发生在C#中,当我在循环中处理线程并且迭代变量在线程体中扮演一些角色时:

for (int i = 0; i < 10; i++) {
  new Thread(new ThreadStart(delegate() { Console.WriteLine(i); })).Start();
}

Will produce unexpected results - mostly 9's instead of 0 to 9, however: 会产生意想不到的结果 - 大多数是9而不是0到9,但是:

for (int i = 0; i < 10; i++) {
  int j = i;
  new Thread(new ThreadStart(delegate() { Console.WriteLine(j); })).Start();
}

Will work fine. 会工作得很好。

I tried something similar. 我尝试了类似的东西。 In my Javascript code I wrote: 在我的Javascript代码中,我写道:

divID++;
localDivID = divID;

and used localDivID instead of divID when setting up the stuff in the DOM. 在DOM中设置东西时使用localDivID而不是divID But this had absolutely no effect. 但这绝对没有效果。 I'm still unable to reference the elements with the ID suffix I want. 我仍然无法使用我想要的ID后缀引用元素。

You need to create a local variable. 您需要创建一个局部变量。
Declare the variable (using the var keyword) inside the function. 在函数内声明变量(使用var关键字)。

Note that Javascript does not have block scope; 请注意,Javascript没有块范围; you can't do this inside a loop. 你不能在循环中做到这一点。

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

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