javascript jQuery 的 $().each() 的排序顺序是否有保证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25155384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:04:18  来源:igfitidea点击:

Is the sort order of jQuery's $().each() guaranteed?

javascriptjqueryhtmlsorting

提问by Simba

On one page of my site I have have a list of entries created by the user - standard HTML <ul>with <li>elements inside it.

在我网站的一个页面上,我有一个由用户创建的条目列表 - 标准 HTML,<ul>其中包含<li>元素。

I want to iterate through the list, but the order of elements is important.

我想遍历列表,但元素的顺序很重要。

Using jQuery $('.myList li').each(), can I guarantee that I will get the lielements in the order that they appear in the DOM?

使用 jQuery $('.myList li').each(),我能保证我会li按照它们在 DOM 中出现的顺序获取元素吗?

From my tests so far, it appears that they do get iterated in the correct order, but I can't find anything that tells me it's guaranteed.

到目前为止,从我的测试来看,它们似乎确实以正确的顺序进行了迭代,但我找不到任何告诉我这是有保证的。

If it isn't guaranteed, what would be the next best alternative method for iterating through them in order?

如果不能保证,那么按顺序迭代它们的下一个最佳替代方法是什么?

回答by Simba

So after reading the docs and coming away still not quite certain, I ended up diving in an actually reading the jQuery source code (thanks to @RoryMcCrossan's answer for prompting me on that).

因此,在阅读了文档并且离开后仍然不太确定,我最终开始实际阅读 jQuery 源代码(感谢@RoryMcCrossan 的回答提示我这样做)。

In fact (contrary to what @RoryMcCrossan said), $().each()uses either for...inor for, depending on whether the input is an object or an array.

事实上(与@RoryMcCrossan 所说的相反),$().each()使用for...infor具体取决于输入是 object 还是 array

For 'array', it suffices to be an 'array-like' object, which is the case for a jQuery object because it contains a numbered list of elements and a lengthproperty.

对于 'array',它足以是一个 'array-like' 对象,这是 jQuery 对象的情况,因为它包含一个编号的元素列表和一个length属性。

Therefore, a call to $().each()will use forand not for...eachas it is iterating over a jQuery object. And since we're using for, we know that we can guarantee the order of iteration for $().each()will match the order of the elements it is given.

因此,调用$().each()将使用for而不是for...each因为它正在迭代一个 jQuery 对象。并且由于我们使用的是for,我们知道我们可以保证 for 的迭代顺序$().each()将匹配给定元素的顺序。

So that leads me to ask a follow-up question of whether the order of elements given by the original query is guaranteed to be the same as they appear in the DOM. If so, then I should be okay.

所以这导致我提出一个后续问题,即原始查询给出的元素顺序是否保证与它们​​出现在 DOM 中的顺序相同。如果是这样,那我应该没问题。

The answer to that can be found in the question linked in the comments by @Mritunjay, and the answer is 'yes, they are returned in the order that they appear in the DOM.

答案可以在@Mritunjay 评论中链接问题中找到,答案是'是的,它们按照它们在 DOM 中出现的顺序返回。

So the final answer is that yes, I can use $('.myList li').each()and iterate through the list items in the order that they appear in the DOM.

所以最终的答案是,是的,我可以$('.myList li').each()按照列表项在 DOM 中出现的顺序使用和迭代它们。

Thanks for the help and the prompts guys. Much appreciated.

感谢您的帮助和提示。非常感激。