Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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中存储for循环的索引_Javascript_Android_For Loop_Indexing - Fatal编程技术网

在javascript中存储for循环的索引

在javascript中存储for循环的索引,javascript,android,for-loop,indexing,Javascript,Android,For Loop,Indexing,我正在用phonegap开发一个android应用程序。我正在制作一个HTML表格,其中一些表格带有来自localStorage的for循环。对于每一行,我需要存储for的索引I,以便使用它从localStorage中检索名为index的项。我有一些代码,但是我为这种效果定义的变量会被循环覆盖(当然)。代码如下: <script language="javascript"> if(len != 0) { var table = document.getElementById('hor

我正在用phonegap开发一个android应用程序。我正在制作一个HTML表格,其中一些表格带有来自localStorage的
for
循环。对于每一行,我需要存储
for
的索引I,以便使用它从localStorage中检索名为index的项。我有一些代码,但是我为这种效果定义的变量会被循环覆盖(当然)。代码如下:

<script language="javascript">
if(len != 0) {
var table = document.getElementById('hor-minimalist-b'); // get the table element
var tableBody = table.childNodes[1]; // get the table body
var tableHead = table.childNodes[0]; // get the table head
var thead = document.createElement('th');
var row2 = document.createElement('tr'); // create a new row
var headText = document.createTextNode('Dados');
  thead.scope = "col";
    thead.appendChild(headText);
    row2.appendChild(thead);
    tableHead.appendChild(row2);

for (var i=0; i<len; i++) {

    var row = document.createElement('tr'); // create a new row

    var cell = document.createElement('td'); // create a new cell
    var a = document.createElement('a');
    var cellText = document.createTextNode(localStorage.getItem('key' + i));
    var xyz = "key" + i;    
    a.href = "alterar.html";
    a.onclick = function() { doLocalStorage(xyz) };
    a.appendChild(cellText);
    cell.appendChild(a); // append input to the new cell
    row.appendChild(cell); // append the new cell to the new row
    tableBody.appendChild(row); // append the row to table body
   }}
</script>
</table>

如果(len!=0){
var table=document.getElementById('hor-minimalist-b');//获取表元素
var tableBody=table.childNodes[1];//获取表体
var tableHead=table.childNodes[0];//获取表头
var thead=document.createElement('th');
var row2=document.createElement('tr');//创建新行
var headText=document.createTextNode('Dados');
thead.scope=“col”;
附加子项(标题文本);
第2行。附加子项(thead);
表头。追加子项(第2行);

对于(var i=0;i尝试将关键字名称放入闭包:

函数包装器(i){
返回函数(){
doLocalStorage(“键”+i)
}}
a、 onclick=wrapper(i);

我不确定您的问题是否正确,但如果您想在执行for循环时异步绑定变量的用法,则应将其包装在闭包中:

    for(i = 1, c = arr.length; i < c; i++){
      (function(i){
        // i wont change inside this closure so bound events will retain i
        $('#id'+i).click(function(){
          alert(i); // Will alert the corresponding i
        })
      })(i);
    }
for(i=1,c=arr.length;i
我对你的问题感到困惑…你为什么需要存储它?你需要将它存储在哪里?你可以将它设置为全局变量或某个元素的属性吗?我应该将此函数放置在哪里?在for中?谢谢!我将其放置在for中并立即工作!非常感谢:)不客气。你可以将包装器定义移到循环之外。
    for(i = 1, c = arr.length; i < c; i++){
      (function(i){
        // i wont change inside this closure so bound events will retain i
        $('#id'+i).click(function(){
          alert(i); // Will alert the corresponding i
        })
      })(i);
    }