JavaScript通过类名和标签名称获取元素 [英] JavaScript get elements by class name and tag name
本文介绍了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')
这篇关于JavaScript通过类名和标签名称获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文