HTML DOM hasAttribute() Yöntemi
❮ Öğe NesnesiÖrnek
Bir <button> öğesinin onclick özniteliğine sahip olup olmadığını öğrenin:
var x = document.getElementById("myBtn").hasAttribute("onclick");
Aşağıda daha fazla "Kendiniz Deneyin" örnekleri.
Tanım ve Kullanım
hasAttribute() yöntemi, belirtilen öznitelik varsa true, aksi takdirde false döndürür.
İpucu: Yeni bir öznitelik eklemek veya bir öğedeki mevcut bir özniteliğin değerini değiştirmek için setAttribute() kullanın .
Tarayıcı Desteği
Tablodaki sayılar, yöntemi tam olarak destekleyen ilk tarayıcı sürümünü belirtir.
Method | |||||
---|---|---|---|---|---|
hasAttribute() | Yes | 9.0 | Yes | Yes | Yes |
Sözdizimi
element.hasAttribute(attributename)
parametreler
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute you want to check if exists |
Teknik detaylar
Geri dönüş değeri: | Boolean, öğenin öznitelikleri varsa true, aksi takdirde false döndürür |
---|---|
DOM Sürümü | Çekirdek Seviye 2 Öğe Nesnesi |
Daha fazla örnek
Örnek
Bir <a> öğesinin hedef özniteliği olup olmadığını öğrenin. Öyleyse, hedef özniteliğin değerini "_self" olarak değiştirin:
// Get the <a> element with id="myAnchor"
var x = document.getElementById("myAnchor");
// If the <a> element has a target attribute, set the value to "_self"
if (x.hasAttribute("target")) {
x.setAttribute("target", "_self");
}
İlgili Sayfalar
HTML Eğitimi: HTML Nitelikleri
HTML DOM Referansı: getAttribute() Yöntemi
HTML DOM Referansı: removeAttribute() Yöntemi
HTML DOM Referansı: setAttribute() Yöntemi
❮ Öğe Nesnesi