Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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_Nested Loops - Fatal编程技术网

Javascript 为什么嵌套for循环没有完成?

Javascript 为什么嵌套for循环没有完成?,javascript,nested-loops,Javascript,Nested Loops,我有一个嵌套的for循环,它没有完成/退出 它正陷入第二个循环: for (var j = 0; next.className != "rfccs" && next !== null; j++) 只有当“next”为空时,它才会被卡住 我的代码: var filtertypes = document.getElementsByClassName("rfccs"); // filtertypes is an array of all filter type headers var

我有一个嵌套的for循环,它没有完成/退出 它正陷入第二个循环:

for (var j = 0; next.className != "rfccs" && next !== null; j++)
只有当“next”为空时,它才会被卡住

我的代码:

var filtertypes = document.getElementsByClassName("rfccs"); // filtertypes is an array of all filter type headers
var next = null; // placeholder for the next sibling element
var tout = document.getElementById("testout"); //test
tout.innerHTML = "init "; //test

for (var i = 0; i < filtertypes.length; i++) {
    filtertypes[i].className += " fhead" + i; // adds a unique class to every filter type header div
    var filtertype = filtertypes[i]; // sets filtertype to the current filter type header
    next = filtertype.nextElementSibling; // gets the next sibling

    for (var j = 0; next.className != "rfccs" && next !== null; j++) { // adds the same class name to all filters in the same filter type
        next.className += " ftype" + i;
        next.innerHTML += i;
        next = next.nextElementSibling;
        tout.innerHTML += "i = " + i + "; j = " + j + "///";
        if (next == null) {
            tout.innerHTML += "DONE";
        }
    }
    tout.innerHTML += "~~~~";
}
var filtertypes=document.getElementsByClassName(“rfccs”);//filtertypes是所有筛选器类型标头的数组
var next=null;//下一个同级元素的占位符
var tout=document.getElementById(“testout”)//测试
tout.innerHTML=“init”//测试
对于(变量i=0;i
我知道我的跟踪/调试代码非常混乱

这是你的电话号码

在检查
.className

next !== null && next.className !== "rfccs" // false if null
此外,由于任何HTMLElement都会被逻辑运算符验证,因此您可以跳过
!==完全为空

next && next.className !== "rfccs" // falsy if `next` falsy
在检查
.className

next !== null && next.className !== "rfccs" // false if null
此外,由于任何HTMLElement都会被逻辑运算符验证,因此您可以跳过
!==完全为空

next && next.className !== "rfccs" // falsy if `next` falsy
解决办法是

for (var j = 0; next != null && next.className != "rfccs"; j++)
如果
next为空
next.className
将失败,从而导致javascript循环

看看这是否是你所期望的 解决方案是

for (var j = 0; next != null && next.className != "rfccs"; j++)
如果
next为空
next.className
将失败,从而导致javascript循环

看看这是否是你所期望的

如果
next
null
,代码将失败,因为首先发生的事情是它将检查属性
next.className
。你需要交换支票。@DerekThomasTran:除了mzdeler所说的之外。这里是MDN文档的链接,因为它很好地解释了这些操作员执行检查的顺序。我希望您会发现这有助于防止将来出现类似问题:)如果
next
null
,您的代码将失败,因为发生的第一件事是它将检查属性
next.className
。你需要交换支票。@DerekThomasTran:除了mzdeler所说的之外。这里是MDN文档的链接,因为它很好地解释了这些操作员执行检查的顺序。我希望您会发现这有助于防止将来出现类似问题:)