
- 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 - 頭元素
- 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 - Modernizr
- 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 - getAttribute() 方法
HTML DOM 的getAttribute() 方法用於獲取屬於 HTML 元素的指定屬性的值。如果給定的屬性不存在,則返回的值將為“null”。
語法
以下是 HTML DOM getAttribute() 方法的語法:
element.getAttribute(attributeName)
引數
此方法接受如下所示的單個引數:
引數 | 描述 |
---|---|
attributeName | 要檢索其值的屬性的名稱。 |
返回值
此方法返回一個字串,其中包含指定屬性的值。如果屬性不存在,則返回“null”。
示例 1:訪問和顯示“href”屬性
以下是 HTML DOM getAttribute() 方法的基本示例。它從錨元素 (<a>) 獲取href 屬性:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <h4>Retrieves the value of href attribute</h4> <a id="myLink" href="https://tutorialspoint.tw">Example Link</a> <div id="output"></div> <script> // Selecting the anchor element by ID const myLink = document.getElementById('myLink'); const hrefVal = myLink.getAttribute('href'); // Displaying the result const od= document.getElementById('output'); od.textContent = `Link URL: ${hrefVal}`; </script> </body> </html>
示例 2:獲取 HTML 元素的屬性值
以下是 HTML DOM getAttribute() 方法的另一個示例。我們使用此方法獲取<div> 元素中所有可用的屬性:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <div id="myElement" class="box" data-info="example data"> <p>This is a paragraph inside a div element.</p> <a href="https://tutorialspoint.tw">Example Link</a> </div> <div id="output"></div> <script> // Selecting the element by ID const my= document.getElementById('myElement'); //Retrieving attribute values const cv = my.getAttribute('class'); const dInv = my.getAttribute('data-info'); const hv = my.querySelector('a').getAttribute('href'); // Displaying the results const od = document.getElementById('output'); od.innerHTML = ` <p>Class attribute value: ${cv}</p> <p>Data-info attribute value: ${dInv}</p> <p>Anchor href attribute value: ${hv}</p> `; </script> </body> </html>
示例 3:獲取按鈕屬性
此示例顯示了 DOM getAttribute() 方法的用法,用於訪問和顯示 HTML <button> 元素的 onclick 屬性的值:
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <h4>Retrieves the value of Button attribute</h4> <button id="myB" onclick="displayMessage()">Click me</button> <div id="ot"></div> <script> function displayMessage() { const ocv = document.getElementById ('myB').getAttribute('onclick'); document.getElementById('ot').textContent = `Value of onclick attribute: ${ocv}`; } </script> </body> </html>
支援的瀏覽器
方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
getAttribute() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告