- 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 屬性指定的屬性
HTML DOM 屬性的 **specified** 屬性用於檢查是否指定了提到的屬性。如果指定了提到的屬性,則返回 true,否則會丟擲錯誤。
語法
attribute.specified
返回值
如果在該標籤中提到了該屬性,則 `specified` 屬性返回 true,否則程式停止執行。
HTML DOM 屬性“specified”屬性的示例
以下示例說明如何在 JavaScript 和 HTML 中使用 `specified` 屬性。
檢查是否指定了 onclick 屬性
以下程式碼演示瞭如何使用 `specified` 屬性來檢查 HTML 元素的屬性是否被顯式指定。
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Attribute specified Property</title>
</head>
<body>
<h3>HTML DOM Attribute specified Property</h3>
<div id="Div"
style="background-color:blue;height:50px"
onclick="">
</div>
<p>Is the onclick attribute specified?</p>
<p id="par"></p>
<script>
document.getElementById("par").innerHTML =
document.getElementById("Div").getAttributeNode("onclick").specified;
</script>
</body>
</html>
未提及屬性時使程式崩潰
以下程式碼顯示了當我們嘗試檢查不存在的屬性的存在時會發生什麼。它在顯示兩個結果後崩潰。
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Attribute specified Property</title>
</head>
<body>
<h3>HTML DOM Attribute specified Property</h3>
<div id="TestDiv"
style="background-color:red;height:50px"
onclick="">
</div>
<p>
Are the following attributes specified for
the element?
</p>
<p id="onclickResult"></p>
<p id="idResult"></p>
<p id="classResult"></p>
<p id="styleResult"></p>
<script>
var element = document.getElementById("TestDiv");
// Check if the 'onclick' attribute is specified
document.getElementById("onclickResult").innerHTML
= "onclick: " +
element.getAttributeNode("onclick").specified;
// Check if the 'id' attribute is specified
document.getElementById("idResult").innerHTML
= "id: " +
element.getAttributeNode("id").specified;
// Check if the 'class' attribute is specified
// We didnt mentioned class attribute for div
// tag, hence program crashes here, you can't.
// see result of class and style attributes.
document.getElementById("classResult").innerHTML
= "class: " +
element.getAttributeNode("class").specified;
// Check if the 'style' attribute is specified
document.getElementById("styleResult").innerHTML
= "style: " +
element.getAttributeNode("style").specified;
</script>
</body>
</html>
處理程式崩潰
在這裡,我們將瞭解如何處理程式崩潰。在嘗試使用 三元運算子 訪問 specified 屬性之前,我們將檢查屬性是否存在。
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Attribute specified Property</title>
</head>
<body>
<h3>HTML DOM Attribute specified Property</h3>
<div id="TestDiv"
style="background-color:red;height:50px"
onclick="">
</div>
<p>
Are the following attributes specified for
the element?
</p>
<p id="onclickResult"></p>
<p id="idResult"></p>
<p id="classResult"></p>
<p id="styleResult"></p>
<script>
var element = document.getElementById("TestDiv");
// Check if the 'onclick' attribute is specified
var onclickAttr = element.getAttributeNode("onclick");
document.getElementById("onclickResult").innerHTML
= "onclick: " + (onclickAttr ? onclickAttr.specified : false);
// Check if the 'id' attribute is specified
var idAttr = element.getAttributeNode("id");
document.getElementById("idResult").innerHTML
= "id: " + (idAttr ? idAttr.specified : false);
// Check if the 'class' attribute is specified
var classAttr = element.getAttributeNode("class");
document.getElementById("classResult").innerHTML
= "class: " + (classAttr ? classAttr.specified : false);
// Check if the 'style' attribute is specified
var styleAttr = element.getAttributeNode("style");
document.getElementById("styleResult").innerHTML
= "style: " + (styleAttr ? styleAttr.specified : false);
</script>
</body>
</html>
支援的瀏覽器
| 屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| specified | 是 | 是 | 是 | 是 | 是 |
廣告




