最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter objects by className from nodeList - Stack Overflow

programmeradmin2浏览0评论

I've got a nodelist of objects returned by a call to document.getElementsByClassName('a'). Some of these objects have an extra class b. How can I create an array of elements that contain both classes? I don't was use a global search for b. I prefer vanillaJS/ES6 code.

HTML code example:

<div class ="a"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>

I've got a nodelist of objects returned by a call to document.getElementsByClassName('a'). Some of these objects have an extra class b. How can I create an array of elements that contain both classes? I don't was use a global search for b. I prefer vanillaJS/ES6 code.

HTML code example:

<div class ="a"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>
Share Improve this question edited Jan 20, 2017 at 20:56 Sᴀᴍ Onᴇᴌᴀ 8,2978 gold badges37 silver badges60 bronze badges asked Jan 20, 2017 at 20:39 ClassicErrorClassicError 1851 gold badge1 silver badge9 bronze badges 3
  • 1 Your question is a little ambiguous. Do you want a result that has elements of both classes, or do you want a result that only has elements with both classes. – user1106925 Commented Jan 20, 2017 at 20:45
  • Sorry that I wasn't specific. Clearly I want class 'a' nodelist index of objects containing 'b' class. – ClassicError Commented Jan 20, 2017 at 20:56
  • Clearly? I'm still confused. How about showing what the result should be? – user1106925 Commented Jan 20, 2017 at 20:59
Add a comment  | 

2 Answers 2

Reset to default 9

Try querySelector instead.

document.querySelectorAll('.a.b');

This is likely your simplest solution.

The documentation for document.getElementsByClassName() states it can accept a string with multiple class names (separated by a space):

 var elements = document.getElementsByClassName(names); // or:
 var elements = rootElement.getElementsByClassName(names);
  • elements is a live HTMLCollection of found elements.
  • names is a string representing the list of class names to match; class names are separated by whitespace

and also in the Examples section:

Get all elements that have both the 'red' and 'test' classes:

document.getElementsByClassName('red test')

That function can be used for both class names. Generally using querySelectorAll() can be slower than using getElementsByClassName() for a simple selector with class names. See comparison in this jsPerf . Related: querySelector and querySelectorAll vs getElementsByClassName and getElementById in JavaScript

const abElements = document.getElementsByClassName('a b');
console.log(abElements);
<div class="a"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>


If you have an existing collection from document.getElementsByClassName() with only the elements that have class name a then the filter() method can be used to keep only the elements that have the second class, utilizing Element.classList.contains() to only include elements with both class names. See a demonstration below. Because document.getElementsByClassName() returns a returns a live NodeList you will need to put the elements into an array before you can call that filter method.

ES-6 Version

The spread syntax can be used to put the elements into an array before calling the filter method.

const aElements = document.getElementsByClassName('a');
const abElements = [...aElements].filter(element => element.classList.contains('b'));
console.log(abElements);
<div class="a"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>

ES-5 Version

Note that Element.classList has limited browser support (e.g. not in IE 9-) so in order to check for the b class without using that property, you could split the class names (using property className) into an array and use Array.indexOf():

var aElements = document.getElementsByClassName('a');
var abElements = Array.prototype.filter.call(aElements, function(element, index, aElements) {
  var classNames = element.className.split(' ');
  return classNames.indexOf('b') > -1;
});
console.log(abElements);
<div class="a"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>
<div class="a b"></div>
<div class="a"></div>

发布评论

评论列表(0)

  1. 暂无评论