JavaScript通过类名和标签名称获取元素
问题描述:
我想通过JavaScript中的类名和标签名来获取DOM元素的列表,而不是使用jQuery。
I wanted to get a list of DOM elements by class name and tag name in JavaScript, not using jQuery.
例如我需要获取所有< ul>
元素与类 .active
For example I need to get all <ul>
elements with class .active
var elements = document.getElementsByTagName("ul");
var activeElements = document.getElementsByClassName('active')
什么是最快最好的在纯JavaScript中做到这一点。
What is the fastest and best way to do this in pure JavaScript.
没有外部图书馆,如jQuery或 cheerio
No external libraries like jQuery or cheerio
答
document.querySelectorAll('ul.active')