
- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引用
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 圖片地圖
- HTML - 內嵌框架 (Iframe)
- HTML - 短語元素
- HTML - 元標籤
- HTML - 類
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表格標題與說明
- HTML - 表格樣式
- HTML - 表格 Colgroup
- HTML - 巢狀表格
- HTML 列表
- HTML - 列表
- HTML - 無序列表
- HTML - 有序列表
- HTML - 定義列表
- HTML 連結
- HTML - 文字連結
- HTML - 圖片連結
- HTML - 郵箱連結
- HTML 顏色名稱與值
- HTML - 顏色名稱
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表單
- HTML - 表單
- HTML - 表單屬性
- HTML - 表單控制元件
- HTML - 輸入屬性
- HTML 媒體
- HTML - 影片元素
- HTML - 音訊元素
- HTML - 嵌入多媒體
- HTML 頭部
- HTML - Head 元素
- HTML - 新增 Favicon
- HTML - Javascript
- HTML 佈局
- HTML - 佈局
- HTML - 佈局元素
- HTML - 使用 CSS 進行佈局
- HTML - 響應式設計
- HTML - 符號
- HTML - 表情符號
- HTML - 樣式指南
- HTML 圖形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 儲存
- HTML - 伺服器傳送事件
- HTML 其他
- HTML - 文件物件模型 (DOM)
- HTML - MathML
- HTML - 微資料
- HTML - IndexedDB
- HTML - Web 訊息傳遞
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音訊播放器
- HTML - 影片播放器
- HTML - 網頁幻燈片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二維碼
- HTML - Modernizer
- HTML - 驗證
- HTML - 顏色拾取器
- HTML 參考
- HTML - 速查表
- HTML - 標籤參考
- HTML - 屬性參考
- HTML - 事件參考
- HTML - 字型參考
- HTML - ASCII 碼
- ASCII 碼錶查詢
- HTML - 顏色名稱
- HTML - 實體
- MIME 媒體型別
- HTML - URL 編碼
- 語言 ISO 程式碼
- HTML - 字元編碼
- HTML - 已棄用標籤
- HTML 資源
- HTML - 快速指南
- HTML - 有用資源
- HTML - 顏色程式碼生成器
- HTML - 線上編輯器
HTML - DOM querySelectorAll() 方法
HTML DOM 的querySelectorAll()方法允許您選擇和訪問文件中與給定 CSS 選擇器匹配的所有 HTML 元素。
CSS 選擇器用於查詢或選擇文件中您想要設定樣式或操作的元素。例如,您可以使用 ID、類名和各種其他選擇器來定位特定元素。
語法
以下是 HTML DOM querySelectorAll() 函式的語法:
element.querySelectorAll(CSS_selectors);
引數
此方法接受如下所述的一個引數:
引數 | 描述 |
---|---|
CSS_selectors | 定義您想要選擇的元素型別的字串。它可以是 ID、類、元素型別(例如 <div> 或 <p>),甚至是特定的屬性及其值。要定義多個選擇器,請用逗號 (,) 分隔它們。 |
返回值
此方法返回一個 NodeList 物件,其中包含與給定 CSS 選擇器匹配的元素集合。如果找不到匹配的元素,則返回一個空的“NodeList”。
示例 1:將樣式應用於所有段落
以下是 HTML DOM querySelectorAll() 方法的基本示例。它將樣式應用於文件中的所有段落 (<p>) 元素:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> <style> .highlight { background-color: lightblue; } </style> </head> <body> <p>Applying Style to All Paragraphs.</p> <p>Click the below button to add style</p> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <button onclick="applyHighlight()">Apply Highlight</button> <script> function applyHighlight() { const para=document.querySelectorAll('p'); para.forEach(para => { para.classList.add('highlight'); }); } </script> </body> </html>
示例 2:計數和顯示列表項
此示例使用 querySelectorAll() 方法計算並顯示無序列表中的列表項 (<li>) 的總數:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> </head> <body> <p>Click the button to see the count of List Items..</p> <ul id="myL"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <button onclick="countItems()">Count Items</button> <p id="totalItems"></p> <script> function countItems() { const i=document.querySelectorAll('#myL li'); document.getElementById('totalItems').textContent = `Total items: ${i.length}`; } </script> </body> </html>
示例 3:向所有元素新增事件監聽器
此示例演示如何使用 querySelectorAll() 方法選擇元素並動態地向所有元素新增事件監聽器:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> </head> <body> <p>Adding Event Listener to all element<p> <button id="btn1">Button 1</button> <button id="btn2">Button 2</button> <button id="btn3">Button 3</button> <div id="output"></div> <script> const bn=document.querySelectorAll('button'); const opd = document.getElementById('output'); bn.forEach(button => { button.addEventListener('click', () => { const buttonId = button.id; const message = `Button ${buttonId} clicked`; // Display message in output const msgP=document.createElement('p'); msgP.textContent = message; opd.appendChild(msgP); }); }); </script> </body> </html>
示例 4:選擇和修改選定的表格單元格
此示例演示如何使用 querySelectorAll() 方法選擇和修改表格單元格。程式碼包含一個按鈕,單擊該按鈕時,它會將突出顯示類新增到所有表格單元格:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> <style> .highlight { background-color: violet; } </style> </head> <body> <p>Click button to apply style.</p> <table id="myT" border="1"> <tr> <td>Name</td> <td>Age</td> </tr> <tr> <td>John</td> <td>30</td> </tr> <tr> <td>Jane</td> <td>25</td> </tr> </table> <button onclick="highlightCells()">Highlight Cells</button> <script> function highlightCells() { const c=document.querySelectorAll('#myT td'); c.forEach(c => { c.classList.add('highlight'); }); } </script> </body> </html>
示例 5:篩選和設定特定元素的樣式
此示例演示如何使用 querySelectorAll() 方法選擇特定元素並根據其位置應用不同的樣式:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> <style> .even { color: green; } .odd { color: blue; } </style> </head> <body> <p>Selects specific elements and applies style to each... </p> <ul id="MyL"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <button onclick="styleNumbers()">Style Numbers</button> <script> function styleNumbers() { const i=document.querySelectorAll('#MyL li'); i.forEach((i, index) => { if ((index + 1) % 2 === 0) { i.classList.add('even'); } else { i.classList.add('odd'); } }); } </script> </body> </html>
示例 6:檢查所有複選框的狀態
此示例演示如何使用 querySelectorAll() 方法檢查所有複選框的狀態。程式碼包含附加到 <div> 元素的 <p> 元素,以顯示每個複選框的狀態:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> </head> <body> <p>Check the status of the following checkboxes</p> <p>Click the below button</p> <input type="checkbox" id="checkbox1"> <label for="checkbox1">Checkbox 1</label><br> <input type="checkbox" id="checkbox2"> <label for="checkbox2">Checkbox 2</label><br> <input type="checkbox" id="checkbox3"> <label for="checkbox3">Checkbox 3</label><br> <button onclick="checkStates()">Check States</button> <div id="checkboxStatus"></div> <script> function checkStates() { const checkboxes = document.querySelectorAll ('input[type="checkbox"]'); const checkboxStatusDiv=document.getElementById ('checkboxStatus'); checkboxStatusDiv.innerHTML = ''; checkboxes.forEach(checkbox => { const checkboxState = checkbox.checked ? 'checked' : 'not checked'; const checkboxStatus = document.createElement('p'); checkboxStatus.textContent = `${checkbox.id} is ${checkboxState}.`; checkboxStatusDiv.appendChild (checkboxStatus); }); } </script> </body> </html>
示例 7:選擇和設定“ul”元素的樣式
此示例演示如何使用 querySelectorAll() 方法選擇和設定多個元素的樣式。程式碼的目標是 <ul> 元素,並將突出顯示類應用於其所有子 <li> 元素:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> <style> .highlight { background-color: violet; } </style> </head> <body> <p>Click button to highlight the <ul> elements... </p> <ul id="myL"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <button onclick="applyHighlight()">Apply Highlight</button> <script> function applyHighlight() { const i=document.querySelectorAll('#myL li'); i.forEach(i => { i.classList.add('highlight'); }); } </script> </body> </html>
示例 8:更新所有“li”項的文字
此示例演示如何使用 querySelectorAll() 方法更改與 CSS 選擇器匹配的所有元素的文字內容:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM querySelectorAll() Method</title> </head> <body> <p>Change the text of all elements by clicking the below button.. </p> <ul id="mL"> <li>Chota</li> <li>Mighty</li> <li>Little</li> </ul> <button onclick="changeText()">Change Text</button> <div id="output"></div> <script> function changeText() { const i=document.querySelectorAll('#mL li'); const names = ['Chota Bheem','Mighty Raju','Little Krishna']; const opd = document.getElementById('output'); opd.innerHTML = ''; i.forEach((item, index) => { item.textContent = names[index]; // Display updated text in output const newip=document.createElement('p'); newip.textContent = `Changed text to: ${names[index]}`; opd.appendChild(newip); }); } </script> </body> </html>
支援的瀏覽器
方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
querySelectorAll() | 是 | 是 | 是 | 是 | 是 |