|
| 1 | +<?xml version="1.0"?> |
| 2 | +<entry type="method" name="uniqueSort" return="jQuery"> |
| 3 | + <title>.uniqueSort()</title> |
| 4 | + <signature> |
| 5 | + <added>3.7</added> |
| 6 | + </signature> |
| 7 | + <desc>Sorts a jQuery object of DOM elements, in place, with the duplicates removed. Note that this only works on jQuery objects consisting of DOM elements, not strings or numbers.</desc> |
| 8 | + <longdesc> |
| 9 | + <p>The <code>.uniqueSort()</code> function searches through a jQuery object, sorting it in document order, and removing any duplicate nodes. A node is considered a duplicate if it is the <em>exact same</em> node as one already in the jQuery object; two different nodes with identical attributes are not considered to be duplicates. This function only works on jQuery objects consisting of DOM elements.</p> |
| 10 | + </longdesc> |
| 11 | + <example> |
| 12 | + <desc>Removes any duplicate elements from the jQuery object of divs.</desc> |
| 13 | + <code><![CDATA[ |
| 14 | +var divs = $( "div" ).get(); |
| 15 | +
|
| 16 | +// Add 3 elements of class dup too (they are divs) |
| 17 | +divs = divs.concat( $( ".dup" ).get() ); |
| 18 | +
|
| 19 | +// Create a jQuery object from `divs`. |
| 20 | +var elems = $( divs ); |
| 21 | +
|
| 22 | +$( "div" ) |
| 23 | + .eq( 1 ) |
| 24 | + .text( "Pre-uniqueSort there are " + elems.length + " elements in the collection." ); |
| 25 | +
|
| 26 | +elems = elems.uniqueSort(); |
| 27 | +
|
| 28 | +$( "div" ) |
| 29 | + .eq( 2 ) |
| 30 | + .text( "Post-uniqueSort there are " + elems.length + " elements in the collection." ) |
| 31 | + .css( "color", "red" ); |
| 32 | +]]></code> |
| 33 | + <css><![CDATA[ |
| 34 | + div { |
| 35 | + color: blue; |
| 36 | + } |
| 37 | +]]></css> |
| 38 | + <html><![CDATA[ |
| 39 | +<div>There are 6 divs in this document.</div> |
| 40 | +<div></div> |
| 41 | +<div class="dup"></div> |
| 42 | +<div class="dup"></div> |
| 43 | +<div class="dup"></div> |
| 44 | +<div></div> |
| 45 | +]]></html> |
| 46 | + </example> |
| 47 | + <example> |
| 48 | + <desc>Locate all the divs preceding the last item and wrap them with a div with class <code>wrapper</code> - with or without <code><a href="/uniqueSort/">.uniqueSort()</a></code>.</desc> |
| 49 | + <code><![CDATA[ |
| 50 | +$( "#container-1" ) |
| 51 | + .find( ".item" ) |
| 52 | + .last() |
| 53 | + .prevAll() |
| 54 | + .wrapAll( "<div class='wrapper' data-content='No uniqueSort'></div>" ); |
| 55 | +
|
| 56 | +$( "#container-2" ) |
| 57 | + .find( ".item" ) |
| 58 | + .last() |
| 59 | + .prevAll() |
| 60 | + .uniqueSort() |
| 61 | + .wrapAll( "<div class='wrapper' data-content='With uniqueSort'></div>" ); |
| 62 | +]]></code> |
| 63 | + <css><![CDATA[ |
| 64 | + body { |
| 65 | + display: flex; |
| 66 | + } |
| 67 | + .container { |
| 68 | + display: flex; |
| 69 | + margin: 10px 50px 10px 10px; |
| 70 | + } |
| 71 | + .wrapper { |
| 72 | + position: relative; |
| 73 | + display: flex; |
| 74 | + padding: 30px 10px 10px 10px; |
| 75 | + background: #def; |
| 76 | + border: 2px solid black; |
| 77 | + } |
| 78 | + .wrapper::before { |
| 79 | + content: attr(data-content); |
| 80 | + position: absolute; |
| 81 | + top: 15px; |
| 82 | + left: 15px; |
| 83 | + } |
| 84 | + .item { |
| 85 | + display: flex; |
| 86 | + justify-content: center; |
| 87 | + align-items: center; |
| 88 | + width: 70px; |
| 89 | + height: 70px; |
| 90 | + background: #abc; |
| 91 | + border: 2px solid black; |
| 92 | + margin: 10px; |
| 93 | + font-size: 50px; |
| 94 | + } |
| 95 | +]]></css> |
| 96 | + <html><![CDATA[ |
| 97 | +<div class="container" id="container-1"> |
| 98 | + <div class="item">1</div> |
| 99 | + <div class="item">2</div> |
| 100 | + <div class="item">3</div> |
| 101 | +</div> |
| 102 | +
|
| 103 | +<div class="container" id="container-2"> |
| 104 | + <div class="item">1</div> |
| 105 | + <div class="item">2</div> |
| 106 | + <div class="item">3</div> |
| 107 | +</div> |
| 108 | +]]></html> |
| 109 | + </example> |
| 110 | + <category slug="utilities"/> |
| 111 | + <category slug="version/3.7"/> |
| 112 | +</entry> |
0 commit comments