小白教程
所有教程
关于
Search
172.69.59.108
172.69.59.108
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Met document queryselectorall”的源代码
本页内容
上一节:
Met_document_queryselector
下一节:
Prop_doc_readystate
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:document.querySelectorAll()}}[[Category:dom obj document|35]] = HTML DOM querySelectorAll() 方法 = [[dom obj document|Document 对象]] 获取文档中 class="example" 的所有元素: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll(".example"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> == 定义与用法 == querySelectorAll() 方法返回文档中匹配指定 CSS 选择器的所有元素,返回 [[js htmldom nodelist|NodeList]] 对象。 NodeList 对象表示节点的集合。可以通过索引访问,索引值从 0 开始。 '''提示:''' 你可以使用 NodeList 对象的 [[prop nodelist length|length]] 属性来获取匹配选择器的元素属性,然后你可以遍历所有元素,从而获取你想要的信息。 更多 CSS 选择器可以参考 [[css syntax|CSS 选择器教程]] , [[css selectors|CSS 选择器参考手册]]。 == 浏览器支持 == 表格中的数字表示支持该方法的第一个浏览器的版本号。 {| class="table table-striped table-hover" ! 方法 ! Chrome ! Internet Explorer / Edge ! Firefox ! Safari ! Opera |- | querySelectorAll() | 4.0 | 8.0 | 3.5 | 3.2 | 10.0 |} '''注意:''' Internet Explorer 8 支持 CSS2 选择器。 IE9 及更高版本的浏览器已经支持 CSS3 选择器。 == 语法 == <pre> elementList = document.querySelectorAll(selectors); </pre> * elementList 是一个静态的 NodeList 类型的对象。 * selectors 是一个由逗号连接的包含一个或多个 CSS 选择器的字符串。 == 属性值 == {| class="table table-striped table-hover" ! 参数 ! 类型 ! 描述 |- | CSS 选择器 | String | 必须。 指定一个或多个匹配 CSS 选择器的元素。可以通过 id, class, 类型, 属性, 属性值等作为选择器来获取元素。 多个选择器使用逗号(,)分隔。 '''提示:''' CSS 选择器更多内容可以参考 [[css selectors|CSS 选择器参考手册]]。 |} == 方法 == {| class="table table-striped table-hover" | DOM 版本: | Level 1 Document Object |- | 返回值: | 一个 NodeList 对象,表示文档中匹配指定 CSS 选择器的所有元素。 NodeList 是一个静态的 NodeList 类型的对象。如果指定的选择器不合法,则抛出一个 SYNTAX_ERR 异常。 |} == 更多实例 == 获取文档中所有的 <p> 元素, 并为匹配的第一个 <p> 元素 (索引为 0) 设置背景颜色: <sample title="" desc="" lang="html" hererun="1"> // 获取文档中所有的 <p> 元素 var x = document.querySelectorAll("p"); // 设置第一个 <p> 元素的背景颜色 x[0].style.backgroundColor = "red"; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 获取文档中所有 class="example" 的 <p> 元素, 并为匹配的第一个 <p> 元素 (索引为 0) 设置背景颜色: <sample title="" desc="" lang="html" hererun="1"> // 获取文档中所有 class="example" 的 <p> 元素 var x = document.querySelectorAll("p.example"); // 设置 class="example" 的第一个 <p> 元素的背景颜色 x[0].style.backgroundColor = "red"; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 计算文档中 class="example" 的 <p> 元素的数量(使用 NodeList 对象的 length 属性): <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll(".example").length; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 设置文档中所有 class="example" 元素的背景颜色: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll(".example"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 设置文档中所有 <p> 元素的背景颜色: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll("p"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 查找文档中共包含 "target" 属性的 <a> 标签,并为其设置边框: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll("a[target]"); var i; for (i = 0; i < x.length; i++) { x[i].style.border = "10px solid red"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> a[target] { background-color: yellow; } </style> </head> <body> <p> CSS 选择器 a[target] 确保所有包含有 target 属性的 a 标签都设置背景颜色。</p> <a href="http://www.xiaobai.wang">小白教程</a> <a href="https://www.google.com" target="_blank">Google</a> <a href="http://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> </run> 查找每个父元素为 <div> 的 <p> 元素,并为其设置背景颜色: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll("div > p"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> 给文档中所有的 <h2>, <div> 和 <span> 元素设置背景颜色: <sample title="" desc="" lang="html" hererun="1"> var x = document.querySelectorAll("h2, div, span"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</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> </run> == 相关文章 == CSS 教程: [[css syntax|CSS 选择器]] CSS 参考手册: [[css selectors|CSS 选择器参考手册]] JavaScript 教程: [[js htmldom nodelist|JavaScript HTML DOM 节点列表]] HTML DOM 参考手册: [[met document queryselector|document.querySelector()]] HTML DOM 参考手册: [[met element queryselectorall|element.querySelectorAll()]] [[dom obj document|Document 对象]]
返回至“
Met document queryselectorall
”。
上一节:
Met_document_queryselector
下一节:
Prop_doc_readystate