简体   繁体   English

JavaScript函数陷入循环

[英]Javascript function going into a loop

I have this below function in my project:- 我的项目中有以下功能:-

function RowSelection() {
  var table = document.getElementById("tbl_Audit");
  if (table != null) {
    for (var x = 0; x < table.rows.length; x++) {
      for (var j = 0; j < table.rows[x].cells.length; j++) {
        if (j = 4) {
          alert("condition passed " + j)
          table.rows[x].cells[j].onclick = function() {
            tableText(this);
          }
        }
      }
    }
  }
}

when this function is triggered it goes into a never ending loop. 当触发此功能时,它将进入永无止境的循环。 Can anybody please help me identify what I am doing wrong here. 谁能帮我确定我在这里做错了什么。

Change if (j=4) to if (j===4) . if (j=4)更改为if (j===4)

= is an assignment, which isn't a syntax error so the code runs, but it changes the value of j to 4 , so (assuming 4 is less than the number of cells) your loop condition will always be true. =是一个赋值,它不是语法错误,因此代码可以运行,但是它将j的值更改为4 ,因此(假设4小于单元格数)您的循环条件将始终为true。

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

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