运行curdivr.toggleClass()之后为什么我的for()循环会转义;

时间:2017-09-08 16:08:16

标签: javascript

我在函数中有一个for()循环。它被设置为运行256次,但每当我在一个元素上运行toggleClass()时它就会离开for循环而不是实际运行toggleClass()。这是我的JS:

newGenAlg();
function newGenAlg(){ 
  var currid = 0;
  var e = document.getElementById('canvas'); // whatever you want to append the rows to: 
  for(var i = 0; i < 16; i++){ 
    for(var x = 0; x < 16; x++){ 
        var cell = document.createElement("div"); 
        //cell.className = "gridsquare"; 
        //cell.position = "absolute";
        cell.style.left = x * 32 + "px";
        cell.innertext = currid;
        //cell.width = 16;
        cell.style.top = i * 32 + "px";
        //cell.height = 16;
        cell.className = "gridsquare";
        cell.id = currid;
        currid++;
        e.appendChild(cell); 
    } 
  } 
  genPath();
}
function genPath() {
  cid = 0;
  for (i = 0; i < 256; i++) {
    curdivc = document.getElementById(i);
    curdivc.innerText = "c";
    curdivr = document.getElementById(i + 1);
    curdivr.innerText = "r";
    curdivd = document.getElementById(i + 16);
    curdivd.innerText = "d";
    detDivDR(curdivr, curdivd, curdivc);

    //cid++;
  }
}
function detDivDR(cdr, cdd, cd) {
  if(true) {
      if(rng(1,4)){
        cdr.toggleClass("gridsquare2");
      } else {
        cdd.toggleClass("gridsquare2");
      } 

    }
}

1 个答案:

答案 0 :(得分:0)

以下是有人回复的解决方案:

  

toggleClass是一个jQuery方法?试试cdr.classList.toggle(&#34; gridsquare2&#34;);