当前位置: 代码网 > it编程>编程语言>Javascript > javascript DOM querySelectorAll() 使用方法

javascript DOM querySelectorAll() 使用方法

2024年05月15日 Javascript 我要评论
queryselectorall定义与用法queryselectorall() 方法返回文档中匹配指定 css 选择器的所有元素,返回nodelist对象。nodelist 对象表示节点的集合。可以通

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com