javascript?DOM?querySelectorAll()?使用方法
querySelectorAll定義與用法
querySelectorAll() 方法返回文檔中匹配指定 CSS 選擇器的所有元素,返回 NodeList 對象。
NodeList 對象表示節(jié)點的集合??梢酝ㄟ^索引訪問,索引值從 0 開始。
提示: 你可以使用 NodeList 對象的 length 屬性來獲取匹配選擇器的元素屬性,然后你可以遍歷所有元素,從而獲取你想要的信息。
瀏覽器支持
表格中的數(shù)字表示支持該方法的第一個瀏覽器的版本號。
表格中的數(shù)字表示支持該方法的第一個瀏覽器的版本號。
方法 | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
querySelectorAll() | 4.0 | 8.0 | 3.5 | 3.2 | 10.0 |
注意: Internet Explorer 8 支持 CSS2 選擇器。 IE9 及更高版本的瀏覽器已經(jīng)支持 CSS3 選擇器。
語法
elementList = document.querySelectorAll(selectors);
- elementList 是一個靜態(tài)的 NodeList 類型的對象。
- selectors 是一個由逗號連接的包含一個或多個 CSS 選擇器的字符串。
屬性值
參數(shù) | 類型 | 描述 |
---|---|---|
CSS 選擇器 | String | 必須。 指定一個或多個匹配 CSS 選擇器的元素??梢酝ㄟ^ id, class, 類型, 屬性, 屬性值等作為選擇器來獲取元素。 多個選擇器使用逗號(,)分隔。 提示: CSS 選擇器更多內(nèi)容可以參考 CSS 選擇器參考手冊。 |
方法
DOM 版本: | Level 1 Document Object |
---|---|
返回值: | 一個 NodeList 對象,表示文檔中匹配指定 CSS 選擇器的所有元素。 NodeList 是一個靜態(tài)的 NodeList 類型的對象。如果指定的選擇器不合法,則拋出一個 SYNTAX_ERR 異常。 |
實例
為 class="example" (索引為 0) 的第一個元素設(shè)置背景顏色
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>腳本之家</title> </head> <body> <h2 class="example">使用 class="example" 的標(biāo)題</h2> <p class="example">使用 class="example" 的段落</p> <p>點擊按鈕為 class="example" (索引為 0) 的第一個元素設(shè)置背景顏色。</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) 設(shè)置背景顏色:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>querySelectorAll 腳本之家(jb51.net)</title> </head> <body> <p>這是一個 p 元素。</p> <p>這也是一個 p 元素。</p> <p>點擊按鈕為文檔中第一個 p (索引為 0) 元素設(shè)置背景顏色。</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) 設(shè)置背景顏色:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>querySelectorAll 腳本之家(jb51.net)</title> </head> <body> <h2 class="example">使用 class="example" 的標(biāo)題</h2> <p class="example">使用 class="example" 的段落</p> <p class="example">另外一個使用 class="example" 的段落</p> <p>點擊按鈕為第一個 class="example" (索引為 0) 的 p 元素設(shè)置背景顏色。</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> 元素的數(shù)量(使用 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" 的元素的數(shù)量。</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>
設(shè)置文檔中所有 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>
設(shè)置文檔中所有 <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> 標(biāo)簽,并為其設(shè)置邊框:
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 標(biāo)簽都設(shè)置背景顏色。</p> <a rel="external nofollow" >菜鳥教程</a> <a rel="external nofollow" target="_blank">Google</a> <a rel="external nofollow" target="_top">wikipedia.org</a> <p>點擊按鈕為所有包含 target 屬性的 a 標(biāo)簽設(shè)置邊框。</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> 元素,并為其設(shè)置背景顏色:
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> 元素設(shè)置背景顏色:
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>點擊按鈕設(shè)置使用 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>
到此這篇關(guān)于javascript DOM querySelectorAll() 使用方法的文章就介紹到這了,更多相關(guān)js querySelectorAll內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
怎樣在JavaScript里寫一個swing把數(shù)據(jù)插入數(shù)據(jù)庫
在JavaScript里寫一個swing來實現(xiàn)確定取消,來決定是否執(zhí)行這個功能 控制把數(shù)據(jù)插入數(shù)據(jù)庫,接下來介紹實現(xiàn)方法2012-12-12Javascript Math ceil()、floor()、round()三個函數(shù)的區(qū)別
Round是四舍五入的。。。Ceiling是向上取整。。float是向下取整2010-03-03Javascript & DHTML 實例編程(教程)DOM基礎(chǔ)和基本API
Javascript & DHTML 實例編程(教程)DOM基礎(chǔ)和基本API...2007-06-06jQuery中文入門指南,翻譯加實例,jQuery的起點教程
jQuery中文入門指南,翻譯加實例,jQuery的起點教程...2007-02-02對JavaScript的全文搜索實現(xiàn)相關(guān)度評分的功能的方法
這篇文章主要介紹了對JavaScript的全文搜索實現(xiàn)相關(guān)度評分的功能的方法,采用了一個名為Okapi BM25的算法,文中亦有介紹,需要的朋友可以參考下2015-06-06表單元素的submit()方法和onsubmit事件應(yīng)用概述
表單元素擁有submit方法,同時也具有onsubmit事件句柄,用于監(jiān)聽表單提交??梢允褂胑lemForm.submit();方法觸發(fā)表單提交,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02