
- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引用
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 圖片地圖
- HTML - 內嵌框架
- 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 - 頭元素
- HTML - 新增 Favicon
- HTML - Javascript
- HTML 佈局
- HTML - 佈局
- HTML - 佈局元素
- HTML - 使用 CSS 進行佈局
- HTML - 響應式設計
- HTML - 符號
- HTML - 表情符號
- HTML - 樣式指南
- HTML 圖形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - 地理位置 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 - Web幻燈片
- 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 屬性
HTML DOM 的attributes 屬性允許您訪問屬於 HTML 元素的屬性集合。此集合作為 NamedNodeMap 返回,允許您使用其名稱與屬性互動並檢索其值。
NamedNodeMap 是一個物件,它儲存元素屬性的集合。它允許我們透過名稱索引訪問屬性,並支援各種操作。
語法
以下是 HTML DOM 的attributes 屬性的語法:
node.attributes
引數
由於它是一個屬性,因此不接受任何引數。
返回值
此屬性返回包含 HTML 元素屬性集合的 'NameNodeMap',如果元素沒有屬性則返回 'null'
示例 1:修改 p 元素的屬性
以下示例演示了 HTML DOM 的attributes 屬性的用法。它修改了<p> 元素的內容:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM attributes</title> <style> .highlight { background-color: yellow; } </style> </head> <body> <p></p>Modifies attributes of the P Element</p> <p id="myParagraph">Example Text</p> <button onclick="modifyParagraph()">Modify</button> <script> function modifyParagraph() { var paragraph = document.getElementById("myParagraph"); paragraph.setAttribute("title", "Modified"); paragraph.textContent = "Modified Text"; paragraph.classList.add("highlight"); } </script> </body> </html>
示例 2:檢查屬性是否存在
以下是 HTML DOM 的attributes 屬性的另一個示例。此屬性用於檢查段落元素上是否存在“title”屬性:
<!DOCTYPE html> <html> <head> <title>HTML DOM Attributes</title> </head> <body> </p>Checking for attribute Existence</p> <p id="myParagraph" title="Example Title">Example Text</p> <button onclick="checkAttributeExistence()">Check Attribute</button> <script> function checkAttributeExistence() { var paragraph = document.getElementById("myParagraph"); var hasTitle = paragraph.hasAttribute("title"); alert("Title Attribute Exists: " + hasTitle); } </script> </body> </html>
示例 3:從 HTML 元素檢索屬性列表
下面的示例檢索並顯示 HTML <div> 元素的所有屬性。單擊按鈕時,所有屬性都將以列表格式顯示:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM attributes</title> </head> <body> <div id="ex-Div"class="example" data-info="ex-data"> Hit the button to see the Div Elements... </div> <button onclick="showAttributes()">Show Attributes</button> <ul id="attributeList"></ul> <script> function showAttributes() { const element = document.getElementById('ex-Div'); const attributeList = document.getElementById('attributeList'); attributeList.innerHTML = ''; for (let attr of element.attributes) { const li = document.createElement('li'); li.textContent = `${attr.name}: ${attr.value}`; attributeList.appendChild(li); } } </script> </body> </html>
示例 4:獲取特定屬性值
此示例從 ID 為“myParagraph”的段落元素中檢索 title 屬性的值,並在警報中顯示它:
<!DOCTYPE html> <html> <head> <title>HTML DOM attributes</title> </head> <body> <p>Getting Specific Attribute</p> <p id="myParagraph" title="Example Title">Example Text</p> <button onclick="getAttributeValue()">Get Attribute Value</button> <script> function getAttributeValue() { var paragraph = document.getElementById("myParagraph"); var titleValue =paragraph.getAttribute("title"); alert("Title Attribute Value: " + titleValue); } </script> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
attributes | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告