JQuery标签去重与数组去重

如图所示

JQuery标签去重与数组去重

代码

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () { //标签去重
$.each($(".drop"), function (key, val_1) {
var count = 0;
$.each($(".drop"), function (key, val_2) {
if ($(val_1).text() == $(val_2).text()) {
count = count + 1;
if (count >= 2) {
$(this).remove();
}
}
});
}); //数组去重
var arr = new Array(2009, 2009, 2010, 2010, 2009, 2010);
arr.sort();//排序
jQuery.unique(arr);//去重
$.each(arr, function (key, val) {
console.log('index in arr:' + key + ", corresponding value:" + val);
});
});
</script>
</head>
<body>
<div>标签去重</div>
<div>
<span class="drop">张三</span>
<span class="drop">李四</span>
<span class="drop">王五</span>
<span class="drop">张三</span>
<span class="drop">王五</span>
</div> </body>
</html>
上一篇:python 中转义字符的注释


下一篇:使用JS来实现验证码功能