Does jQuery always iterate through DOMs in order in which they are found in the code?

前端 未结 3 690
囚心锁ツ
囚心锁ツ 2021-02-19 12:58

Say, for example, I have a sortable list:

  • Item 1
  • Item 2
  • Item 3
相关标签:
3条回答
  • 2021-02-19 13:50

    "Can I expect jQuery to go through the entries from top to bottom?"

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

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

    "I've run a few tests, and tried rearranging items dynamically..."

    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.

    0 讨论(0)
  • 2021-02-19 13:55

    There is no where in the docs that promise this behavior, but this the way it's currently implemented.

    That said, I can't see a reason why would they change this behavior.

    0 讨论(0)
  • 2021-02-19 14:00

    It didn't use to, but since jQuery 1.3.2 selectors return elements in the order in which they are found in the DOM.

    0 讨论(0)
提交回复
热议问题