JavaScript DOM querySelectorAll() 方法详解

作者:交互手记时间:2026-10-30 20:10:59浏览:97

本文详细介绍 JavaScript 中 querySelectorAll() 方法的定义、语法及浏览器兼容性。通过多个实际代码示例,演示如何利用该方法获取匹配 CSS 选择器的元素集合,并展示如何遍历 NodeList 对象进行样式修改或属性统计,适用于前端开发中的 DOM 元素批量操作场景。

querySelectorAll() 方法的定义与基本用法

该方法用于选取文档中符合指定 CSS 选择器的所有元素,并返回一个 NodeList 对象。

NodeList 对象代表节点的集合,其元素可以通过从 0 开始的索引进行访问。

提示:可以利用 NodeList 对象的 length 属性获取匹配元素的总数,并通过遍历该集合提取所需信息。

浏览器兼容性支持情况

下表列出了支持该方法的首个浏览器版本号。

下表列出了支持该方法的首个浏览器版本号。

方法ChromeEdgeFirefoxSafariOpera
querySelectorAll()4.08.03.53.210.0

注意:Internet Explorer 8 仅支持 CSS2 选择器;IE9 及以上版本已支持 CSS3 选择器。

语法规范

elementList = document.querySelectorAll(selectors);
  • elementList 为静态 NodeList 类型的对象。
  • selectors 为由逗号分隔的一个或多个 CSS 选择器组成的字符串。

返回值说明

参数类型描述
CSS 选择器String必须。 指定一个或多个匹配 CSS 选择器的元素。可以通过 id, class, 类型, 属性, 属性值等作为选择器来获取元素。

多个选择器使用逗号(,)分隔。

提示: CSS 选择器更多内容可以参考 CSS 选择器参考手册。

方法特性

DOM 版本:Level 1 Document Object
返回值:一个 NodeList 对象,表示文档中匹配指定 CSS 选择器的所有元素。 NodeList 是一个静态的 NodeList 类型的对象。如果指定的选择器不合法,则抛出一个 SYNTAX_ERR 异常。

代码实例

为 class="example" (索引为 0) 的第一个元素设置背景颜色

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本站</title>
</head>
<body>
<h2 class="example">使用 class="example" 的标题</h2>
<p class="example">使用 class="example" 的段落</p> 
<p>点击按钮为 class="example" (索引为 0) 的第一个元素设置背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

获取文档中所有的 <p> 元素, 并为匹配的第一个 <p> 元素 (索引为 0) 设置背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
</head>
<body>
<p>这是一个 p 元素。</p>
<p>这也是一个 p 元素。</p>
<p>点击按钮为文档中第一个 p (索引为 0) 元素设置背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

获取文档中所有 class="example" 的 <p> 元素, 并为匹配的第一个 <p> 元素 (索引为 0) 设置背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
</head>
<body>
<h2 class="example">使用 class="example" 的标题</h2>
<p class="example">使用 class="example" 的段落</p> 
<p class="example">另外一个使用 class="example" 的段落</p> 
<p>点击按钮为第一个 class="example" (索引为 0) 的 p 元素设置背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p.example");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

计算文档中 class="example" 的 <p> 元素的数量(使用 NodeList 对象的 length 属性):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
</head>
<body>
<div class="example">
使用 class="example" 的 div 元素
</div>
<div class="example">
另一个使用 class="example" 的 div 元素
</div>
<p class="example">使用 class="example" 的 p 元素</p>
<p>点击按钮计算 class="example" 的元素的数量。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    document.getElementById("demo").innerHTML = x.length;
}
</script>
</body>
</html>

设置文档中所有 class="example" 元素的背景颜色:

var x = document.querySelectorAll(".example");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

完整实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
<style>
.example {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>
<div class="example">
使用 class="example" 的 div
</div>
<div class="example">
另外一个使用 class="example" 的 div
</div>
<p class="example">使用 class="example" 的 p 元素</p>
<p>这是一个使用了 class="example" 的 <span class="example">span</span> 元素,它在 p 元素里面。</p>
<p>点击按钮修改所有 class="example" 元素的背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

设置文档中所有 <p> 元素的背景颜色:

var x = document.querySelectorAll("p");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

完整实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
<style>
.example {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>
<h1>一个 h1 元素</h1>
<div>一个 div 元素</div>
<p>一个 p 元素。</p>
<p>另一个 p 元素。</p>
<div class="example">
  <p>在 div 中的一个 p 元素。</p>
</div>
<p>点击按钮修改文档中所有 p 元素的背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

查找文档中共包含 "target" 属性的 <a> 标签,并为其设置边框:

var x = document.querySelectorAll("a[target]");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.border = "10px solid red";
}

完整实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
<style>
a[target] {
    background-color: yellow;
}
</style>
</head>
<body>
<p> CSS 选择器 a[target] 确保所有包含有  target 属性的 a 标签都设置背景颜色。</p>
<a href="https://www.runoob.com" rel="external nofollow" >菜鸟教程</a>
<a href="https://www.google.com" rel="external nofollow"  target="_blank">Google</a>
<a href="http://www.wikipedia.org" rel="external nofollow"  target="_top">wikipedia.org</a>
<p>点击按钮为所有包含 target 属性的 a 标签设置边框。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("a[target]");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.border = "10px solid red";
    }
}
</script>
</body>
</html>

查找每个父元素为 <div> 的 <p> 元素,并为其设置背景颜色:

var x = document.querySelectorAll("div > p");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

完整实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
<style>
div {
    border: 1px solid black;
    margin-bottom: 10px;
}
</style>
</head>
<body>
<div>
  <h3>H3 元素</h3>
  <p>我是一个 p 元素,我的父元素是 div 元素。</p>
</div>
<div>
  <h3>H3 元素</h3>
  <p>我是一个 p 元素,我的父元素也是 div 元素。</p>
</div>
<p>点击按钮修改父元素为 div 的所有 p 元素的背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("div > p");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

给文档中所有的 <h2>, <div> 和 <span> 元素设置背景颜色:

var x = document.querySelectorAll("h2, div, span");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
}

完整实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 本站(jb51.net)</title>
</head>
<body>
<h1>一个 H1 元素</h1>
<h2>一个 H2 元素</h2>
<div>一个 DIV 元素</div>
<p>一个 p 元素</p>
<p>一个 p 元素,包含了 <span style="color:brown;">span</span> 元素了。 </p>
<p>点击按钮设置使用 h2, div 和 span 元素的背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("h2, div, span");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

到此这篇关于javascript DOM querySelectorAll() 使用方法的文章就介绍到这了,更多相关js querySelectorAll内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

作者最新文章