JavaScript通过类名和标签名获取元素

JavaScript get elements by class name and tag name

本文关键字:标签名 获取 元素 JavaScript      更新时间:2023-09-26

我想在JavaScript中通过类名和标签名获得DOM元素列表,而不是使用jQuery。

例如,我需要获得类别为.active的所有<ul>元素

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

在JavaScript中最快和最好的方法是什么?

没有外部库,如jQuery或cheerio

document.querySelectorAll('ul.active')