JavaScript:一个接一个循环未执行
我有这个JavaScript代码,它是在点击一个按钮后执行的。 for循环执行得很好,但之后的while循环根本不执行。 甚至警报也没有执行JavaScript:一个接一个循环未执行,javascript,loops,Javascript,Loops,我有这个JavaScript代码,它是在点击一个按钮后执行的。 for循环执行得很好,但之后的while循环根本不执行。 甚至警报也没有执行 // FAULTY FUNCTION BEGINNING function swap_table_horizontally_vertically() { var old_table = document.getElementById('synopsis_table'), old_table_rows = o
// FAULTY FUNCTION BEGINNING
function swap_table_horizontally_vertically() {
var old_table = document.getElementById('synopsis_table'),
old_table_rows = old_table.getElementsByTagName('tr'),
cols = old_table_rows.length, rows = old_table_rows[0].getElementsByTagName('td').length,
cell, next, temp_row, i = 0, new_table = document.createElement('table');
// Removes all sort_buttons
for (i = 0; i <= cols; i++) {
var sort_buttons = document.getElementById('sort_buttons');
sort_buttons.parentNode.removeChild(sort_buttons);
}
//NOT EXECUTING FROM HERE
alert("success");
while(i < rows) {
cell = 0;
temp_row = document.createElement('tr');
if (i == 0) {
while(cell < cols) {
next = old_table_rows[cell++].getElementsByTagName('td')[0];
temp_row.appendChild(next);
}
new_table.appendChild(temp_row);
++i;
}
else {
while(cell < cols) {
next = old_table_rows[cell++].getElementsByTagName('td')[0];
temp_row.appendChild(next);
}
new_table.appendChild(temp_row);
++i;
}
}
old_table.parentNode.replaceChild(new_table, old_table);
new_table.setAttribute("id", "synopsis_table");
}
// FAULTY FUNCTION END
//函数开始时出错
函数交换\u表\u水平\u垂直(){
var old_table=document.getElementById('synosis_table'),
old_table_rows=old_table.getElementsByTagName('tr'),
cols=old_table_rows.length,rows=old_table_rows[0]。getElementsByTagName('td')。length,
单元格,下一个,临时行,i=0,新表=document.createElement('table');
//删除所有排序按钮
对于(i=0;i检查第一个循环循环的次数。因为我认为循环一开始就可以通过id“sort\u buttons”删除元素,但是因为使用“id”,它将只找到一个具有该“id”的元素(您只能有一个具有“id”sort\u buttons)的元素)
因此,当第二次循环时,它将找不到具有“id”“排序按钮”的元素
检查控制台中是否有任何错误
提示:在for循环中添加“console.log(i);
”以查看循环的次数。您在for循环中迭代i,因此在第一个循环之后i高于行。您可能希望在第二个循环之前设置i=0或使用不同的变量。在控制台中得到的错误是什么?for
循环条件应该具有