jQuery.unique()

jQuery.unique( array ) 官网原E文: Array

描述: 删除数组中重复元素。只处理删除DOM元素数组,而不能处理字符串或者数字数组。

  • version added: 1.1.3jQuery.unique( array )

    arrayDOM元素的数组。

$.unique()函数通过搜索的数组对象,排序数组,并移除任何重复的节点。此功能只适用于普通的JavaScript DOM元素的数组,主要是jQuery内部使用。

在jQuery 1.4中结果将始终按文档顺序返回。

Example:

Removes any duplicate elements from the array of divs.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.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>

    var divs = $("div").get(); // unique() must take a native array

    // 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>

Demo:

jQuery 1.6 API 中文版Clove整理、修订