gpt4 book ai didi

jQueryeach()非阻塞?

转载 作者:行者123 更新时间:2023-12-01 02:24:21 24 4
gpt4 key购买 nike

jQuery 的each() 函数是非阻塞的吗?

在下面的示例中,我希望首先记录“test in every”,但在每个测试中,首先记录“test out of every”。

因此,这导致循环在返回错误之前运行到主体(有时是文档)元素。

我只是想获得一个共同的父元素来包装,或者如果两个元素(firstp 和 lastp)是兄弟元素,则包装它们。

但是,我最感兴趣的是each()函数的性能以及它是否是非阻塞的。

while(firstp[0] !== lastp[0]){
//If parents are siblings, this will do for wrapping.
var isS = false;
firstp.siblings().each( function(index, el){
console.log("test in each");
if(jQuery(el)[0] === lastp[0]){
isS = true;
return true;
}
});
console.log("test out of each");
if(isS === true){ break; }
firstp = firstp.parent();
lastp = lastp.parent();
}

编辑:

对不起各位。这是一场虚惊。保罗是对的。我的 HTML 实际上有两个与firstp 匹配的元素。我没有意识到这一点,所以第一个没有 sibling ,导致了错误。我一直在看第二个元素。

但是,我确实在这个过程中学到了一些东西,所以感谢您花时间回答。

最佳答案

.each() 是同步的。我的猜测是这样的:

在 while 循环的第一次迭代中,firstp 没有同级,因此每个迭代的对象是空的,并且“测试每个”是第一个记录的内容。然后在第二次迭代中,firstp 有 sibling ,因此它进入each,并且您立即看到“test in every”在“test out of every”之后被打印。

我认为,如果您将代码更改为类似这样的内容,您将从控制台看到正确的行为,这样您就可以看到循环正在进行的迭代:

var i = 1;
while(firstp[0] !== lastp[0]){
//If parents are siblings, this will do for wrapping.

var isS = false;
firstp.siblings().each( function(index, el){
console.log(i+": test in each ("+index+")");
if(jQuery(el)[0] === lastp[0]){
isS = true;
return true;
}
});
console.log(i+": test out of each"));
i++;
if(isS === true){ break; }
firstp = firstp.parent();
lastp = lastp.parent();
}

关于jQueryeach()非阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6906608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com