
- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引用
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 圖片地圖
- HTML - Iframes
- 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 classList 屬性
HTML DOM 的classList 屬性是一個只讀屬性,它提供了一種訪問和操作 HTML 元素類列表的方法。
此屬性返回一個包含指定元素類的DOMTokenList 物件,使您可以輕鬆地新增、刪除、切換和檢查類的存在。
語法
以下是 HTML DOM 的classList 屬性的語法:
element.classList;
引數
由於它是一個屬性,因此不接受任何引數。
返回值
此屬性返回一個 DOMTokenList 物件,該物件表示元素的 class 屬性中的一組類名。
屬性和方法
以下列出了您可以用來向列表中新增、切換或刪除 CSS 類的屬性和方法:
方法/屬性 | 描述 |
---|---|
add() | 透過新增一個或多個類來修改元素的 classList。 |
remove() | 透過刪除一個或多個類來修改元素的 classList。 |
toggle() | 透過切換類開啟或關閉來修改元素的 classList。 |
contains() | 檢查元素的 classList 是否包含特定類。 |
item() | 訪問元素的 classList 中特定索引處的類。 |
replace() | 替換元素的 classList 中的一個類為另一個類。 |
entries() | 返回一個物件,該物件以鍵值對的形式迭代類列表。 |
keys() | 返回一個物件,該物件迭代類列表的每個索引。 |
values() | 返回一個物件,該物件允許迭代類列表的值(類名)。 |
forEach() | 對 classList 中的每個類執行給定的函式一次。 |
length | 返回元素的 classList 中的類數。 |
示例 1:向“div”元素新增新類
以下程式演示了 HTML DOM 的classList 屬性的用法。它向一個<div> 元素新增一個類:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: green; color: white; padding: 10px; } button{ padding: 8px 12px; margin: 10px 0px; } </style> </head> <body> <p>Click the below to add a class ("highlight") to the "div" element.</p> <div id="myDiv">Tutorialspoint ("TP")</div> <button onclick="addClasses()">Add Class</button> <script> function addClasses() { let divElement = document.getElementById('myDiv'); // Adding classes to the element by using add() method divElement.classList.add('highlight'); } </script> </body> </html>
上述程式向“div”元素添加了“highlight”類。
示例 2:從段落(“p”)中刪除指定的類
以下是 HTML DOM 的classList 屬性的另一個示例。我們使用此屬性及其“remove()”方法從段落(<p>)中刪除指定的類:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: green; color: white; padding: 10px; } button{ padding: 8px 12px; } </style> </head> <body> <p>Click the below button to remove highlight from the paragraph.</p> <p id="paragraph" class="highlight">This paragraph has a highlighted background.</p> <button onclick="removeClass()">Remove Highlight Class</button> <script> function removeClass() { var paragraph = document.getElementById("paragraph"); paragraph.classList.remove("highlight"); } </script> </body> </html>
上述程式從段落中刪除了指定的類。
示例 3:在突出顯示段落之間切換
以下示例演示了classList 屬性的“toggle()”方法的用法。此方法允許我們切換段落 (<p>) 內文字的背景顏色:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: yellow; padding: 10px; } </style> </head> <body> <p>Click the button to add or remove the highlight...</p> <p id="paragraph">Click to toggle highlight:</p> <button onclick="document.getElementById('paragraph').classList.toggle('highlight')">Toggle Highlight </button> </body> </html>
上面的程式透過點選按鈕向段落新增和刪除“highlight”類。
示例 4:檢查元素上的類
以下示例使用 classList 屬性的“contains()”方法來檢查元素是否應用了“highlight”類:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: yellow; padding: 10px; } </style> </head> <body> <p>Click the below button to check whether the paragraph contains the "highlight" class.</p> <p id="pg" class="highlight">This paragraph has a highlighted background.</p> <button onclick="checkHighlight()">Check Highlight Class</button> <script> function checkHighlight() { var paragraph = document.getElementById("pg"); if (paragraph.classList.contains("highlight")) { alert("Yes, paragraph contains 'highlight' class"); } else { alert("No, paragraph contains 'highlight' class"); } } </script> </body> </html>
示例 5:訪問特定類
此示例顯示了classList 屬性的“item()”方法的用法,該方法允許我們訪問應用於元素的各個類名:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: green; padding: 10px; color: white; } .italic { font-style: italic; } </style> </head> <body> <h2>Accessing Specific Classes</h2> <p id="paragraph" class="highlight italic">This paragraph has multiple classes.</p> <button onclick="getClassNames()">Get Classes</button> <p id="classOutput"></p> <script> function getClassNames() { var paragraph = document.getElementById("paragraph"); var classList = paragraph.classList; // Accessing specific classes by index var firstClass = classList.item(0); var secondClass = classList.item(1); var thirdClass = classList.item(2); // Displaying the classes document.getElementById("classOutput").textContent="First class: " + firstClass + ", Second class: " + secondClass; } </script> </body> </html>
示例 6:動態替換類
此示例顯示瞭如何使用classList 屬性的“replace()”方法動態地從元素中替換類:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM classList</title> <style> .highlight { background-color: yellow; padding: 10px; } .italic { background-color: green; padding: 10px; color: white; } button{ padding: 8px 10px; } </style> </head> <body> <p id="paragraph" class="highlight">This paragraph has a highlighted background.</p> <button onclick="replaceClass()">Replace Highlight with Italic</button> <script> function replaceClass() { var paragraph = document.getElementById("paragraph"); paragraph.classList.replace("highlight","italic"); } </script> </body> </html>
示例 7:遍歷類
此示例演示瞭如何使用classList 屬性的“forEach()”方法遍歷動態應用於元素的每個類:
<!DOCTYPE html> <html> <head> <title>HTML DOM classList</title> <style> .red { color: red; padding: 10px; } .bold { font-weight: bold; } .underline { text-decoration: underline; } </style> </head> <body> <p>It will iterate over the class values..</p> <div id="ex" class="red bold underline">Dynamic Classes</div> <button onclick="applyForEach()">Apply forEach</button> <div id="output"></div> <script> function applyForEach() { const element = document.getElementById('ex'); let res="<p><strong>ClassList Classes:</strong></p>"; element.classList.forEach(className => { res += `<p>${className}</p>`; }); document.getElementById('output').innerHTML=res; } </script> </body> </html>
示例 8:檢索類數
此示例顯示瞭如何使用 HTML DOM 的classList.length 屬性檢索應用於元素(即<div>)的類數:
<!DOCTYPE html> <html> <head> <title>HTML DOM classList</title> <style> .red { color: red; padding: 10px; } .b { font-weight: bold; } .udl { text-decoration: underline; } button{ padding: 8px 12px; } </style> </head> <body> <p>Click the below button to displays number of classes applied...</p> <div id="ex" class="red b udl">Dynamic Classes</div> <button onclick="displayClassListLength()">Display ClassList Length</button> <div id="output"></div> <script> function displayClassListLength() { const element = document.getElementById('ex'); const classLength = element.classList.length; const res = `<p>Number of Classes: ${classLength}</p>`; document.getElementById('output').innerHTML = res; } </script> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
classList | 是 | 是 | 是 | 是 | 是 |