jQuery.unique()

jQuery.unique( array )返回类型:Arrayversion deprecated: 3.0

描述:排序一个DOM元素的数组,同时删除重复的项。请注意它只对DOM元素的数组起作用,对字符串数组或数字数组不起作用。

自从jQuery 3.0以来,此方法被淘汰了,仅仅是jQuery.uniqueSort()方法的别名。请使用jQuery.uniqueSort()来代替它。

$.unique()方法搜索遍一个对象的数组,排序该数组,删除任何重复的节点。如果一个节点和已经在数组中的某节点完全相同,那么该节点被视为重复的。此函数只对DOM元素的扁平JavaScript数组起作用,主要是在jQuery内部使用。

自从jQuery 1.4以来,此方法的结果将始终按文档顺序返回。你很可能永远都不需要用到它。

示例:

从<div>的数组中删除所有重复的元素。

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.unique 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.unique( divs );
$( "div:eq(2)" ).text( "Post-unique there are " + divs.length + " elements." )
.css( "color", "red" );
</script>
</body>
</html>

演示: