From 86511a77137bf43f4b98a9e855424b8286a743eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?=
Given a jQuery object that represents a set of DOM elements, the The method optionally accepts a selector expression of the same type that we can pass to the The result of this call is a red background behind items 1 and 2. Since we do not supply a selector expression, these preceding elements are unequivocally included as part of the object. If we had supplied one, the elements would be tested for a match before they were included. Note: Many APIs, like The following call: would result in the following HTML: because "Item 2" gets appended to the wrapper div first. To work around the issue, you may use .prevAll()
method searches through the predecessors of these elements in the DOM tree and construct a new jQuery object from the matching elements; the elements are returned in order beginning with the closest sibling.$()
function. If the selector is supplied, the elements will be filtered by testing whether they match it.append
or wrapAll
process node in the order in which they appear in the jQuery object. This can pose issues with APIs like .prevAll()
in which the reverse document order is used. Consider the following example:
+
+<div>
+ <div>First</div>
+ <div>Second</div>
+ <div class="last-item">Last</div>
+</div>
+
+
+$( ".last-item" )
+ .prevAll()
+ .wrapAll( "<div class='wrapper'></div>" );
+
+
+<div>
+ <div class="wrapper">
+ <div>Second</div>
+ <div>First</div>
+ </div>
+ <div class="last-item">Last</div>
+</div>
+
$.uniqueSort()
on the .prevAll()
output first:
+var prevSiblings = $( ".last-item" ).prevAll();
+$.uniqueSort( prevSiblings );
+prevSiblings.wrapAll( "<div class='wrapper'></div>" );
+