- 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 元素 setAttributeNode() 方法
**setAttributeNode()** 方法允許您為特定元素定義特定的屬性節點。它會更新任何現有的屬性節點。
語法
element.setAttributeNode(newAttrNode);
引數
此方法接受如下所述的一個引數。
| 引數 | 描述 |
|---|---|
| newAttrNode | 您要為特定元素定義的新節點。 |
返回值
setAttributeNode() 方法並不總是返回值;相反,它為元素設定新的屬性節點值。但是,如果更新了屬性,則它會返回一個“Attr”物件,該物件儲存更新的屬性節點。
HTML DOM 元素“setAttributeNode()”方法示例
以下是 setAttributeNode() 方法的一些示例,這些示例演示了它在動態修改 HTML 屬性節點中的用法。
在 Div 元素上設定 Class 屬性節點
此示例演示如何使用 setAttributeNode() 方法在 **<div>** 元素上設定 class 屬性。以下程式碼包含一個按鈕,單擊該按鈕時,會動態建立新的 Div 元素。
<!DOCTYPE html>
<html>
<head>
<style>
.setAttr {
color: red;
}
</style>
</head>
<body>
<h1>HTML DOM Element</h1>
<h2>The setAttributeNode() Method</h2>
<p>Click "Change" to set the class attribute
to a div element.
</p>
<button onclick="myFunction()">Change</button>
<script>
function myFunction() {
var divele = document.createElement('div');
divele.textContent =
"This is a dynamically created div element.";
// Create a new class attribute node
var clsatr = document.createAttribute('class');
clsatr.value = 'setAttr';
divele.setAttributeNode(clsatr);
document.body.appendChild(divele);
}
</script>
</body>
</html>
為 Div 元素新增動態顏色更改
此示例演示如何透過單擊按鈕動態更改 <div> 元素的背景顏色。當您單擊“更改顏色”時,<div> 元素的背景顏色將更新為隨機顏色。
<!DOCTYPE html>
<html>
<head>
<title>Change Element Color</title>
</head>
<body>
<h1>HTML DOM Element</h1>
<h2>The setAttributeNode() Method</h2>
<p>Keep clicking "Change Color" to change the
color continuously of the <div> element.
</p>
<button onclick="changeColor()">
Change Color
</button>
<hr>
<div id="myd"style="width:200px;height:200px;
background-color:lightblue;">
This is a div element.
</div>
<script>
function changeColor() {
var ele = document.getElementById('myd');
// Generate a random hex color
var randomColor = '#' + Math.floor
(Math.random()*16777215).toString(16);
// Set the background color using setAttribute()
ele.setAttribute
('style','width:200px;height:200px;background-color:'
+randomColor+';');
}
</script>
</body>
</html>
向現有段落新增自定義屬性
此示例演示如何使用 setAttributeNode() 方法向現有的段落 (**<p>**) 元素新增自定義屬性。以下程式碼包含一個按鈕,單擊該按鈕時,會動態將自定義屬性新增到段落。
<!DOCTYPE html>
<html>
<head>
<title>setAttribute() Example</title>
</head>
<body>
<h1>HTML DOM Element</h1>
<h2>The setAttributeNode() Method</h2>
<p>Click "Add Attribute" to set a custom
attribute on a paragraph.
</p>
<button onclick="addCustomAttribute()">
Add Attribute
</button>
<hr>
<p id="myPara">This is a paragraph element.</p>
<script>
function addCustomAttribute() {
var para= document.getElementById('myPara');
// Set a custom attribute using setAttribute()
para.setAttribute
('data-custom','Hello, this is a custom attribute!');
// Show a message to the user
var msg = document.createElement('p');
msg.textContent = 'Custom attribute added!';
document.body.appendChild(msg);
}
</script>
</body>
</html>
支援的瀏覽器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| setAttributeNode() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告




