admin管理员组

文章数量:1221441

I wanted to get a list of DOM elements by class name and tag name in JavaScript, not using jQuery.

For example I need to get all <ul> elements with class .active

var elements = document.getElementsByTagName("ul");
var activeElements = document.getElementsByClassName('active')

What is the fastest and best way to do this in JavaScript.

No external libraries like jQuery or cheerio

I wanted to get a list of DOM elements by class name and tag name in JavaScript, not using jQuery.

For example I need to get all <ul> elements with class .active

var elements = document.getElementsByTagName("ul");
var activeElements = document.getElementsByClassName('active')

What is the fastest and best way to do this in JavaScript.

No external libraries like jQuery or cheerio

Share Improve this question edited Nov 22, 2017 at 3:16 svnm asked Aug 25, 2015 at 1:21 svnmsvnm 24.3k23 gold badges94 silver badges109 bronze badges 2
  • 3 To be pedantic, in pure JavaScript there's no way to do it, because pure JavaScript doesn't have anything to do with a DOM. But what you're looking for is document.querySelectorAll(). – Pointy Commented Aug 25, 2015 at 1:22
  • Locating DOM elements using selectors – djvg Commented Oct 7, 2020 at 7:23
Add a comment  | 

1 Answer 1

Reset to default 18
document.querySelectorAll('ul.active')

本文标签: domJavaScript get elements by class name and tag nameStack Overflow