jQuery.unique()方法

jQuery 教程 · 2019-03-29 11:58:02

实例

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

<div>document中有6div块</div> <div></div> <div class="dup"></div> <div class="dup"></div> <div class="dup"></div> <div></div> <script> $(function () { // unique() 获取一个原始数组 var divs = $( "div" ).get(); // 添加 3 个div 块元素 divs = divs.concat( $( ".dup" ).get() ); $( "div:eq(1)" ).text( "重排序后有 " + divs.length + " 个元素。" ); divs = jQuery.unique( divs ); $( "div:eq(2)" ).text( "重排序后有 " + divs.length + " 个元素。" ) .css( "color", "red" ); }) </script>

定义和用法

$.unique() 函数用于对DOM元素数组进行排序,并移除重复的元素。

注意:1. 仅适用于DOM元素数组,不能处理字符串或者数字数组。
2. 这里的重复指的是两个元素实际上是同一个元素(通过全等"==="来判断),例如不同节点属性相同的元素不被认为重复的元素。
3. 在jQuery 3.0中,这种方法已被弃用,只是jQuery.uniqueSort()的别名。请使用该方法代替。

语法

$.unique( array )

参数 描述
array Array类型 指定的DOM元素数组。

点击查看所有 jQuery 教程 文章: https://codercto.com/courses/l/35.html

查看所有标签

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具