HTML DOM querySelectorAll() 方法 | HTML DOM Document 对象

返回到:DOM 对象:HTML DOM Document 对象

定义与用法

querySelectorAll() 方法返回文档中匹配指定 CSS 选择器的所有元素,返回 NodeList 对象。

NodeList 对象表示节点的集合。可以通过索引访问,索引值从 0 开始。

提示: 你可以使用 NodeList 对象的 length 属性来获取匹配选择器的元素属性,然后你可以遍历所有元素,从而获取你想要的信息。

浏览器支持

表格中的数字表示支持该方法的第一个浏览器的版本号。

方法谷歌IE火狐苹果opera
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, 类型, 属性, 属性值等作为选择器来获取元素。

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

方法

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

实例

获取文档中 class=”example” 的所有元素:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</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>Web176教程网(web176.com)</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>Web176教程网(web176.com)</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>Web176教程网(web176.com)</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” 元素的背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</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> 元素的背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</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> 标签,并为其设置边框:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
<style>
a[target] {
    background-color: yellow;
}
</style>
</head>
<body>

<p> CSS 选择器 a[target] 确保所有包含有  target 属性的 a 标签都设置背景颜色。</p>

<a href="https://www.runoob.com">菜鸟教程</a>
<a href="https://www.google.com" target="_blank">Google</a>
<a href="//www.wikipedia.org" 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> 元素,并为其设置背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</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> 元素设置背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</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>

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4664.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年12月15日 下午3:01
下一篇 2021年12月15日 下午3:11

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注