
元素對象
檢查按鈕元素是否有 onclick屬性:
document.getElementsByTagName("BUTTON")[0].hasAttribute("onclick");
輸出結果:
true
hasAttribute() 方法用于判斷是否有指定的屬性存在,如果存在傳回 true,否則傳回 false。
提示: 我們可以使用 setAttribute() 來添加一個新屬性,或者修改元素中已存在的屬性。
所有主要浏覽器都支援 hasAttribute() 方法
Internet Explorer 8 及更早 IE 版本不支援該方法。
element.hasAttribute(attributename)
參數
類型
描述
attributename
String
必須。你要檢查的屬性名。
Boolean
如果元素有屬性傳回 true, 否則傳回 false。
DOM 版本
Core Level 2 Element Object
