我这样获得数组的元素:
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
});
但是我不确定该怎么做。如果有人提出建议,我将不胜感激。
您可以遍历元素并将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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句