- 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 - 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 setAttribute() 方法
HTML DOM 的setAttribute() 方法用於為 DOM 中指定的元素新增或更新屬性。HTML 中的屬性是 HTML 元素的特殊特徵或屬性,提供有關該元素的附加資訊。
如果指定的元素不存在,該方法不會建立該元素;相反,如果在不存在的元素上呼叫此方法,則會丟擲錯誤。要為元素設定屬性,必須首先確保該元素存在於 DOM 中。
以下互動式示例演示了 setAttribute() 方法在不同場景中的用法:
歡迎來到 Tutorialspoint..
您來對地方學習了……
- 如果單擊“新增”按鈕,此方法將向第一段新增“style”屬性。
- 如果單擊“更新”按鈕,此方法將更新第一段的現有“style”屬性。
語法
以下是 HTML DOM setAttribute() 方法的語法:
element.setAttribute(attributeName, attributeValue);
引數
此方法接受如下所述的兩個引數:
| 引數 | 描述 |
|---|---|
| attributeName | 要設定或更改的屬性的名稱。 |
| attributeValue | 要為屬性定義的新值。 |
返回值
此方法不返回任何值。
示例 1:為 HTML 元素設定屬性
以下程式演示瞭如何使用 HTML DOM setAttribute() 方法在單擊<button>時將樣式設定為<h2> 元素:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
</head>
<body>
<p>Click the button below to change the color and font size of the heading:</p>
<h2 id="h">Welcome</h2>
<button onclick="changeColor()">Change Color</button>
<script>
function changeColor() {
const heading = document.getElementById('h');
heading.setAttribute('style', 'color: red; font-size: 24px;');
}
</script>
</body>
</html>
示例 2:為 Div 元素設定內聯樣式
以下是 HTML DOM setAttribute() 方法的另一個示例。我們使用此方法透過設定屬性來更改現有<div>的內聯樣式:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
border: 1px solid black;
margin-bottom: 10px;
color: blue;
font-size: 18px;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<h4>Setting Inline Styles</h4>
<div id="ex" class="container">This div has inline Styles. Click below to change my Style!</div>
<button onclick="setInlineStyles()">Change Inline Styles</button>
<script>
function setInlineStyles() {
const ele = document.getElementById('ex');
ele.setAttribute('style', 'color: green; font-size: 24px;');
}
</script>
</body>
</html>
示例 3:在新的元素上建立和設定屬性
以下示例演示了透過建立和設定新元素上的屬性來使用setAttribute() 方法。它建立一個新的 <div> 元素並將其附加到文件正文:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
background-color: #f9f99f;
border: 1px solid black;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<p>Creates and Sets Attributes on a New Element</p>
<button onclick="createAndSetAttributes()">Create New Element</button>
<script>
function createAndSetAttributes() {
const nd = document.createElement('div');
nd.setAttribute('id', 'nd');
nd.setAttribute('class', 'container');
nd.textContent="This is a newly created div.";
document.body.appendChild(nd);
}
</script>
</body>
</html>
示例 4:為 Div 元素設定 Class 屬性
此示例演示了透過在單擊按鈕時向現有 <div> 新增類屬性 (highlight) 來使用setAttribute() 方法:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM setAttribute()</title>
<style>
.container {
padding: 20px;
background-color: #f0f0ff;
border: 1px solid black;
margin-bottom: 10px;
}
.highlight {
background-color: yellow;
}
button{
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>setAttribute() Method</h2>
<h4>Click button to apply class Attribute</h4>
<div id="ex" class="container">This div has class "container".</div>
<button onclick="addClass()">Add Class 'highlight'</button>
<script>
function addClass() {
const ele = document.getElementById('ex');
ele.setAttribute('class','container highlight');
}
</script>
</body>
</html>
支援的瀏覽器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| setAttribute() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告




