hasAttribute fonksiyonu, elementin belirtilen bir özelliğe sahip olup-olmadığını kontrol eder. Özelliğe sahipse true, değilse false döndürür.
Sözdizimi
sonuc = element.hasAttribute(ozellik)
ozellik | Elementin sahip olup-olmadığı öğrenilmek istenen özelliğin adı |
Dönüş Değeri
sonuc | Özelliğe sahipse true, sahip değilse false olur. |
Aşağıdaki örnek, bir table elementinin hasAttribute ile cellspacing özelliğine sahip olup-olmadığı kontrol edilerek, olmaması durumunda setAttribute ile özelliğin eklenmesini göstermektedir.
<input type="button" value="cellspacing Kontrol" onClick="csKontrol()"/>
<table id="ornekTablo" style="border:solid 1px #6a6;" rules="all">
<tr>
<td>Hücre-1</td><td>Hücre-2</td>
</tr>
<tr>
<td>Hücre-3</td><td>Hücre-4</td>
</tr>
</table>
<script type="text/javascript">
function csKontrol()
{
var Tablo = document.getElementById('ornekTablo');
if(Tablo.hasAttribute('cellspacing') == false)
{
Tablo.setAttribute('cellspacing','10');
}
}
</script>
Test Edin