简体   繁体   English

将值分配给变量WHILE循环

[英]Assigning values to variable WHILE loop

I have a WHILE loop that loops through a table as long as $i is less than 10. I want this loop to assign the INNERHTML of each cell to a variable. 只要$ i小于10,我就有一个循环遍历表的WHILE循环。我希望该循环将每个单元格的INNERHTML分配给一个变量。 So that I can use it later on to process the information (the loop is because you can choose if it has to take first 3 or first 10 cells). 这样我以后就可以使用它来处理信息了(循环是因为您可以选择是否必须使用前3个或前10个单元格)。 But how do I assign every innerHTML to a different variable? 但是,如何将每个innerHTML分配给不同的变量? I'm sure there has to be an easy way. 我敢肯定必须有一个简单的方法。 But google couldn't get me a good answer. 但是谷歌不能给我一个很好的答案。

Thanks in advance! 提前致谢!

Milaan Milaan

Arrays are designed for this purpose. 阵列就是为此目的而设计的。

(Presumably you mean i , not $i as Javascript variables do not need to start with $ .. though there's no reason you can't , many people choose to use that only for jQuery objects. I will not use them below. Also I've had to guess at the layout of your code.) (大概你是说i ,而不是$i因为Javascript变量不需要以$ 。开头。尽管没有理由你不能这样做 ,许多人选择仅将其用于jQuery对象。下面我也不会使用它们。不得不猜测代码的布局。)

var myHTMLs = [];
var i = 0;
while (i < 10) {
   var html = *<no idea how you are getting cell contents here.. what cell?!>*
   myHTMLs[myHTMLs.length] = html;
   i++;
}

Can you give each cell a unique id? 您可以为每个单元格指定一个唯一的ID吗? Then you can just do: 然后,您可以执行以下操作:

var cell_ids = ['id-cell-1', 'id-cell-2',...];
var cellcontents = [];

while (i < 10) {
  cellcontents.push($("#" + cell_ids[i]).html());
  i--;
}

an array? 数组?

this is just semi psuedo code but: 这只是半伪代码,但:

var i;
var innerString[];

while (i < 10) {
    innerString[i] = (innerHTML of elementID);
    i++;
}

If I'm reading your question correctly, you want to keep the innerHTML of each cell that you loop over, right? 如果我正确地阅读了您的问题,则希望保留循环的每个单元格的innerHTML,对吗? In that case, you'd probably want to use an array. 在这种情况下,您可能要使用数组。 Maybe like this: 也许是这样的:

var i = 0;
var innerHTML_array = [];
while( i < 10 ) {
  innerHTML_array[i] = current_cell.innerHTML;
  i++;
}

This is when arrays are useful: 这是数组有用的时候:

var i = 0;
var contents = [];
while (i < 10) {
   var div = document.getElementById("foo" + i);
   contents.push(div.innerHTML);
   i++;
}
alert(contents.length);
alert(contents[0]);
alert(contents[1]);

Maybe this will help: 也许这会有所帮助:

Try to reference Your table with some id, eg. 尝试使用一些ID来引用您的表格,例如。

<table id="myTable">

then, try this code 然后,尝试此代码

var tab = document.getElementById("myTable");
var y = 0, x = 0;

var myContainer = [];

for (y = 0; y < tab.rows.length; y++){
    for (x = 0; x < tab.rows[y].cells[x].length; x++){
        if (!myContainer[y])
            myContainer[y] = [];
        myContainer[y][x] = tab.rows[y].cells[x].innerHTML;
    }
}

Your table content is now holded in myContainer variable. 您的表内容现在保存在myContainer变量中。 To access cell no 3 in row no 8 You just use: 要访问第8行中的第3单元格,您只需使用:

alert(myContainer[8][3]);

You can try using jQuery's .map() to loop over all the cells and return their innerHTML properties as an array. 您可以尝试使用jQuery的.map()遍历所有单元格,并将它们的innerHTML属性作为数组返回。

Example: http://jsfiddle.net/xHH64/1/ 示例: http//jsfiddle.net/xHH64/1/

var htmlArray = $('tbody td', '#tableID').map(function(i,v){
  return i < 10 ? v.innerHTML : null;
}).get();

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

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