javascript?DOM?querySelectorAll()?使用方法

 更新时间:2023年06月11日 10:24:15   作者:runoob  
querySelectorAll()?方法返回文档中匹配指定?CSS?选择器的所有元素,返回?NodeList?对象,一般用来获取指定id火class下的所有节点
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

querySelectorAll定义与用法

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内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

最新评论

?


http://www.vxiaotou.com