Metode HTML DOM setAttributeNode()
Objek ElemenContoh
Setel simpul atribut kelas dari elemen <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>
Sebelum mengatur node atribut:
Hello World
Setelah mengatur simpul atribut:
Hello World
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Metode setAttributeNode() menambahkan node atribut yang ditentukan ke sebuah elemen.
Jika atribut yang ditentukan sudah ada, metode ini menggantikannya.
Nilai kembalian dari metode ini adalah objek Attr. Untuk informasi selengkapnya, lihat Objek Atribut DOM HTML .
Lihat juga metode setAttribute() .
Tip: Gunakan metode removeAttributeNode() untuk menghapus node atribut dari sebuah elemen.
Dukungan Peramban
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
Sintaksis
element.setAttributeNode(attributenode)
Nilai Parameter
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
Detail Teknis
Nilai Kembali: | Objek Attr, mewakili simpul atribut yang diganti, jika ada, jika tidak null |
---|---|
Versi DOM | Objek Elemen Level 1 Inti |
Lebih Banyak Contoh
Contoh
Setel simpul atribut href dari elemen <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>
Sebelum mengatur node atribut:
Go to w3schools.com
Setelah mengatur simpul atribut:
Halaman Terkait
Tutorial HTML: Atribut HTML
Referensi HTML DOM: Objek Atribut DOM HTML
Referensi DOM HTML: Metode setAttribute()
Referensi DOM HTML: Metode document.createAttribute()
Referensi DOM HTML: atribut .nilai Properti
Referensi DOM HTML: Metode getAttributeNode()
Referensi DOM HTML: Metode removeAttributeNode()
Objek Elemen