总有人间一两风,填我十万八千梦

jQuery .uniqueSort() 用法 手册 | 示例代码

jQuery开发手册 归档 320℃ 0评论

Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.

  • version added: 1.12-2.2jQuery.uniqueSort( array )

    • array
      Type: Array
      The Array of DOM elements.

The $.uniqueSort() function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates. This function only works on plain JavaScript arrays of DOM elements, and is chiefly used internally by jQuery. You probably will never need to use it.

Prior to jQuery 3.0, this method was called jQuery.unique().

As of jQuery 1.4 the results will always be returned in document order.

Example:

Removes any duplicate elements from the array of divs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.uniqueSort demo</title>
<style>
div {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>There are 6 divs in this document.</div>
<div></div>
<div class="dup"></div>
<div class="dup"></div>
<div class="dup"></div>
<div></div>
<script>
// unique() must take a native array
var divs = $( "div" ).get();
// Add 3 elements of class dup too (they are divs)
divs = divs.concat( $( ".dup" ).get() );
$( "div:eq(1)" ).text( "Pre-unique there are " + divs.length + " elements." );
divs = jQuery.uniqueSort( divs );
$( "div:eq(2)" ).text( "Post-unique there are " + divs.length + " elements." )
.css( "color", "red" );
</script>
</body>
</html>

转载请注明:悠然品鉴 » jQuery .uniqueSort() 用法 手册 | 示例代码

喜欢 (0)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址