• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • jQuery.uniqueSort()

    排序一个DOM元素的数组,恰当的除去重复项。请注意,这仅适用于DOM元素数组,而不能处理字符串或者数字数组。

    jQuery.uniqueSort(array)
    • array类型: Array。DOM元素的数组。

    $.unique()函数通过搜索的数组对象,排序数组,并移除任何重复的节点。如果一个节点和已经在数组中的节点完全相同,那么它被认为是重复的;两个不同的节点具有相同的属性是被认为不重复的。此功能只适用于普通的JavaScript DOM元素的数组,主要是jQuery内部使用。你可能永远都不需要使用它。

    在jQuery 3.0之前,这个方法叫做jQuery.unique().

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

    例子

    从数组中删除任何重复的div元素。

    <!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-3.0.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>
    
    There are 6 divs in this document.