开发者

jQuery: order of elements does not match order in document

Is order of elements r开发者_运维问答eturned from query supposed to match order of elements in document in 1.3.2 version of jQuery. I am doing following select from unordered list.

$el.parents('li')

and it is returning it in reversed order, from bottom up.

$el is three levels deep "li".

I understand that it walks the tree up, but this http://docs.jquery.com/Release:jQuery_1.3.2 explicitly states that elements are returned in the order they appear in document. So is this a bug?


Parents traverses the DOM from the nearest parent to the outermost, which is why it is returned in "reverse". Jquery does not reorder its results ordinarily -- if the method of traversal is top to bottom, the results are returned "in order" (top to bottom). If the method of traversal is inner to outer then the results are returned "out of order".


That is the order of parents for the element, it's the order they come in when looking at parents...the order is by definition backwards.

This is because maybe you want the first <li> parent:

$el.parents('li:eq(0)')


parents is walking from the current node in the DOM up to the root and selects all nodes that match the given expression. So the matches are in reversed order when viewed from the root.


I wasn't able to duplicate what you saw, but a fix would be to reverse the jQuery element array.

jQuery.fn.reverse = [].reverse;

Adds a jQuery reverse function so $el.parents('li').reverse() will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜