我为这个数组获取了这样的元素:
var headerElements = document.getElementsByClassName("headerElement");
我喜欢消除数组中具有相同属性的元素" title",如下所示:
$.each(headerElements, function(i, el){
el.attr('title') is already in the array, remove the element from the array
});
但我不知道该怎么办。如果有人有建议,我将非常感激。
答案 0 :(得分:0)
您可以遍历您的元素并将title
属性存储在Array
中。如果已存储title
属性,请从Array
:
var selected = $('.common');
console.log('The array contains '+selected.length+' elements before any modification.');
var titleList = [];
$('.common').each(function(){
if(!titleList.includes($(this).attr('title'))) titleList.push($(this).attr('title'));
else selected = selected.not($(this));
});
console.log('The final array contains '+selected.length+' elements.');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="common" title="1"></div>
<div class="common" title="1"></div>
<div class="common" title="2"></div>
<div class="common" title="2"></div>
<div class="common" title="3"></div>
<div class="common" title="3"></div>