HTML DOM setAttributeNode() Yöntemi
❮ Öğe NesnesiÖrnek
Bir <h1> öğesinin sınıf öznitelik düğümünü ayarlayın:
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>
Nitelik düğümünü ayarlamadan önce:
Hello World
Nitelik düğümünü ayarladıktan sonra:
Hello World
Aşağıda daha fazla "Kendiniz Deneyin" örnekleri.
Tanım ve Kullanım
setAttributeNode() yöntemi, belirtilen öznitelik düğümünü bir öğeye ekler.
Belirtilen öznitelik zaten mevcutsa, bu yöntem onun yerini alır.
Bu yöntemin dönüş değeri bir Attr nesnesidir. Daha fazla bilgi için bkz . HTML DOM Öznitelik Nesnesi .
Ayrıca setAttribute() yöntemine bakın.
İpucu: Bir öğeden bir öznitelik düğümünü kaldırmak için removeAttributeNode() yöntemini kullanın.
Tarayıcı Desteği
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
Sözdizimi
element.setAttributeNode(attributenode)
Parametre Değerleri
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
Teknik detaylar
Geri dönüş değeri: | Varsa değiştirilen öznitelik düğümünü temsil eden bir Attr nesnesi, aksi takdirde boş |
---|---|
DOM Sürümü | Çekirdek Düzey 1 Öğe Nesnesi |
Daha fazla örnek
Örnek
Bir <a> öğesinin href öznitelik düğümünü ayarlayın:
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>
Nitelik düğümünü ayarlamadan önce:
Go to w3schools.com
Nitelik düğümünü ayarladıktan sonra:
İlgili Sayfalar
HTML Eğitimi: HTML Nitelikleri
HTML DOM Referansı: HTML DOM Nitelik Nesnesi
HTML DOM Referansı: setAttribute() Yöntemi
HTML DOM Referansı: document.createAttribute() Yöntemi
HTML DOM Referansı: nitelik .value Özellik
HTML DOM Referansı: getAttributeNode() Yöntemi
HTML DOM Referansı: removeAttributeNode() Yöntemi
❮ Öğe Nesnesi