
- 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 APIs
- 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 元素 hasAttributes() 方法
hasAttributes() 方法檢查 HTML DOM 中的元素是否具有屬性。如果元素具有一個或多個屬性,則返回“true”,否則返回“false”。
注意:hasAttribute() 方法檢查特定屬性的存在,而 hasAttributes() 方法檢查元素是否根本有任何屬性。
語法
element.hasAttributes();
返回值
此方法返回一個布林值:如果元素具有屬性,則返回“true”;否則返回“false”。
HTML DOM 元素“hasAttributes()”方法示例
以下是一些 hasAttribute() 方法的示例,它檢查 HTML DOM 元素中特定屬性的存在。
檢查 Div 元素是否有任何屬性
此示例演示如何使用 hasAttribute() 方法檢查任何屬性,然後在單擊按鈕時顯示結果。它檢查id=exampleDiv 的<div>元素是否具有任何屬性。
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Element hasAttributes() Method </title> </head> <body> <h1>HTML - DOM Element</h1> <h2>hasAttributes() Method</h2> <div id="exampleDiv" title="Sample Div"> Does it has any attributes? </div> <br> <button onclick="checkAttributes()"> Check Attributes </button> <p id="result"></p> <script> function checkAttributes() { const divElement = document.getElementById ('exampleDiv'); document.getElementById('result').textContent= "The div has attributes."; } </script> </body> </html>
檢查 Body 元素的屬性
此示例檢查 body 元素是否具有任何屬性。由於此示例中的 body 元素沒有任何屬性,因此 hasAttributes() 方法返回 false 並顯示相應的訊息。
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Element hasAttributes() Method </title> </head> <body> <h1>HTML - DOM Element </h1> <h2>hasAttributes() Method</h2> <p>Does Body element has any attribute?</p> <button onclick="checkAttributes()"> Check Body Attributes </button> <p id="result"></p> <script> function checkAttributes() { const bodyElement = document.body; document.getElementById('result').textContent= "Body element does not have any attributes."; } </script> </body> </html>
檢查錨標籤是否具有屬性
此示例檢查錨點(<a>)標籤在 HTML 元素中是否具有任何屬性,並相應地顯示訊息。
<!DOCTYPE html> <html lang="en"> <head> <title> HTML DOM Element hasAttributes() Method </title> </head> <body> <h1>HTML - DOM Element</h1> <h2>hasAttributes() Method</h2> <p>Checks if Anchor tag has any Attributes</p> <a id="myLink" href= "https://tutorialspoint.tw/"> Example Link </a> <button onclick="checkAttributes()"> Check Attributes </button> <p id="result"></p> <script> function checkAttributes() { const linkElement = document.getElementById ('myLink'); document.getElementById('result').textContent = "The link has attributes."; } </script> </body> </html>
支援的瀏覽器
方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
hasAttributes() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告