Select all elements with class="example":
document.querySelectorAll(".example");
Try it Yourself »
More examples below.
The querySelectorAll()
method returns all elements that matches a CSS selector(s).
The querySelectorAll()
method returns a NodeList.
The querySelectorAll()
method throws a SYNTAX_ERR exception if the selector(s) is invalid
The JavaScript Node List Tutorial
The Element querySelector() Method
The Element querySelectorAll() Method
The Document querySelector() Method
The Document querySelectorAll() Method
The Document getElementById() Method
A NodeList is an array-like collection (list) of nodes.
The nodes in the list can be accessed by index. The index starts at 0.
The length Poperty returns the number of nodes in the list.
document.querySelectorAll(
CSS selectors)
Parameter | Description |
CSS selectors | Required. One or more CSS selectors. CSS selectors select HTML elements based on id, classes, types, attributes, values of attributes etc. For a full list, go to our CSS Selectors Reference. For multiple selectors, separate each selector with a comma (See "More Examples"). |
Type | Description |
Object | A NodeList object with the elements that matches the CSS selector(s). If no matches are found, an empty NodeList object is returned. |
Add a background color to the first <p> element:
const nodeList= document.querySelectorAll("p");
nodeList[0].style.backgroundColor = "red";
Try it Yourself »
Add a background color to the first <p> element with class="example":
const nodeList = document.querySelectorAll("p.example");
nodeList[0].style.backgroundColor = "red";
Try it Yourself »
Number of elements with class="example":
let numb = document.querySelectorAll(".example").length;
Try it Yourself »
Set the background color of all elements with class="example":
const nodeList = document.querySelectorAll(".example");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the background color of all <p> elements:
let nodeList = document.querySelectorAll("p");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the border of all <a> elements with a "target" attribute:
const nodeList = document.querySelectorAll("a[target]");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.border = "10px solid red";
}
Try it Yourself »
Set the background color of every <p> element where the parent is a <div> element:
const nodeList = document.querySelectorAll("div > p");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the background color of all <h3> and <span> elements:
const nodeList = document.querySelectorAll("h3, span");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
document.querySelectorAll()
is a DOM Level 3 (2004) feature.
It is fully supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!