jQuery是否总是按照在代码中找到它们的顺序来遍历DOM? [英] Does jQuery always iterate through DOMs in order in which they are found in the code?

查看:60
本文介绍了jQuery是否总是按照在代码中找到它们的顺序来遍历DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个可排序的列表:

Say, for example, I have a sortable list:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

我想知道的是,如果我进行如下所示的jQuery调用:

What I want to know is, if I make a jQuery call such as the following:

$.each($('li'), function(key, item) {...});

我可以期望jQuery从上到下遍历所有条目吗?我已经进行了一些测试,并尝试使用jQueryUI动态地重新排列项目,到目前为止,它似乎总是按顺序运行.但这总是可以预期的吗?还是到目前为止我还没有遇到任何让我想到其他事情的愚蠢的运气??

Can I expect jQuery to go through the entries from top to bottom? I've run a few tests, and tried rearranging items dynamically using jQueryUI, and so far, it seems to always run in order. But can this always be expected? Or is it dumb luck that I've not run into anything so far to make me think otherwise...?

推荐答案

我可以期望jQuery从上到下遍历所有条目吗?"

是的.通过从0length - 1的数字​​索引对其进行迭代.

Yes. They are iterated by numeric index from 0 to length - 1.

元素将始终按照它们在DOM中出现的顺序返回.

The elements will always be returned in the order that they appear in the DOM.

我已经进行了一些测试,并尝试动态地重新排列项目..."

如果这就是您的意思,那么它不会考虑CSS的位置.如果您要更改其在DOM中的实际位置,那么只有从DOM中重新选择更新后,您才能看到这些更新.

It doesn't give consideration to CSS positioning if that's what you mean. If you're changing their actual location in the DOM, then you'd only see the updates if you re-select them from the DOM.

这篇关于jQuery是否总是按照在代码中找到它们的顺序来遍历DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆