Does order of elements in jQuery wrapped set alway

2019-03-10 15:49发布

Is the order of the elements in jQuery wrapped set guaranteed to match the order the elements appear in the markup?

I ask because I need to perform an operation on a set of nested elements, and I need to always do the operation in order of the nesting.

Can I just run the operation using .each iterator on the matched set and I'll always get the right order or do I need to find some other way to guarantee the operation order matches the nesting order?

标签: jquery set wrap
3条回答
ゆ 、 Hurt°
2楼-- · 2019-03-10 16:30

I forget which specific version it was, but JQuery 1.3.x guaranteed that the document order was the order of the resulting elements, even with queries like 'span, div'. (Prior versions did not return the results in document order, and I have needed this several times.)

查看更多
小情绪 Triste *
3楼-- · 2019-03-10 16:48

Just been looking at this myself. jQuery does return things in document order as per the following article:

http://docs.jquery.com/Release:jQuery_1.3.2

So, if you select some ids as such:

$("#id1, #id2, #id3")

Then they will be returned in the order they appear in the DOM, not necessarily in the order they are given. Its certainly worth being aware of this when you upgrade from earlier to later version of jQuery.

查看更多
劫难
4楼-- · 2019-03-10 16:49

jQuery will match your query top down so the matched set will always be the same.

查看更多
登录 后发表回答