HTML DOM setAttributeNode()เมธอด
❮ วัตถุธาตุตัวอย่าง
ตั้งค่าโหนดแอตทริบิวต์คลาสขององค์ประกอบ <h1>:
var h1 = document.getElementsByTagName("H1")[0]; // Get the first <h1> element in the document
var att = document.createAttribute("class"); // Create a "class" attribute
att.value = "democlass"; // Set the value of the class attribute
h1.setAttributeNode(att); // Add the class attribute to <h1>
ก่อนตั้งค่าโหนดแอตทริบิวต์:
Hello World
หลังจากตั้งค่าโหนดแอตทริบิวต์:
Hello World
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
setAttributeNode() วิธีการเพิ่มโหนดแอตทริบิวต์ที่ระบุให้กับองค์ประกอบ
หากมีแอตทริบิวต์ที่ระบุอยู่แล้ว วิธีการนี้จะแทนที่แอตทริบิวต์ดังกล่าว
ค่าที่ส่งกลับของเมธอดนี้คืออ็อบเจ็กต์ Attr สำหรับข้อมูลเพิ่มเติม โปรดดู ที่HTML DOM Attribute Object
ดู เมธอดsetAttribute() ด้วย
เคล็ดลับ:ใช้ เมธอด removeAttributeNode()เพื่อลบโหนดแอตทริบิวต์ออกจากองค์ประกอบ
รองรับเบราว์เซอร์
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
element.setAttributeNode(attributenode)
ค่าพารามิเตอร์
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
รายละเอียดทางเทคนิค
คืนมูลค่า: | วัตถุ Attr แทนโหนดแอตทริบิวต์ที่ถูกแทนที่ หากมี มิฉะนั้นจะเป็นโมฆะ |
---|---|
เวอร์ชัน DOM | วัตถุองค์ประกอบหลักระดับ 1 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ตั้งค่าโหนดแอตทริบิวต์ href ของ <a> องค์ประกอบ:
var anchor = document.getElementById("myAnchor"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "https://www.w3schools.com"; // Set the value of the href attribute
anchor.setAttributeNode(att); // Add the href attribute to <a>
ก่อนตั้งค่าโหนดแอตทริบิวต์:
Go to w3schools.com
หลังจากตั้งค่าโหนดแอตทริบิวต์:
หน้าที่เกี่ยวข้อง
บทช่วยสอน HTML: แอตทริบิวต์ HTML
การอ้างอิง HTML DOM: HTML DOM Attribute Object
การอ้างอิง HTML DOM: setAttribute() Method
การอ้างอิง HTML DOM: document.createAttribute() Method
การอ้างอิง HTML DOM: แอตทริบิวต์ .value คุณสมบัติ
การอ้างอิง HTML DOM: getAttributeNode() Method
การอ้างอิง HTML DOM: removeAttributeNode() Method
❮ วัตถุธาตุ