HTML DOM hasAttribute()Method

元素对象参考手册Element Object

Example

Check if the button element has an onclick attribute:

document.getElementsByTagName("BUTTON")[0].hasAttribute("onclick");

Output:

true

Try it Yourself »

Definition and Usage

The hasAttribute() method is used to determine whether a specified attribute exists. If it exists, it returns true, otherwise it returns false.

Tip: We can usesetAttribute()to add a new attribute, or modify an existing attribute of the element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the hasAttribute() method.

Internet Explorer 8 and earlier IE versions do not support this method.


Syntax

element.hasAttribute(attributename)

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to check.

Return Value

Type Description
Boolean Returns true if the element has the attribute, otherwise returns false.

Technical Details

DOM Version Core Level 2 Element Object


元素对象参考手册Element Object

Other Extensions