HTML DOM setAttribute()Method

元素对象参考手册Element Object

Examples

Set the type attribute of an input element:

document.getElementsByTagName("INPUT")[0].setAttribute("type","button");

Before setting the attribute:

After setting the attribute:


Try it Yourself »

Definition and Usage

The setAttribute() method creates or changes an attribute.

If the specified attribute already exists, only its value is set.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setAttribute() method.

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


Syntax

element.setAttribute(attributename,attributevalue)

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to add.
attributevalue String Required. The value of the attribute you want to add.

Return Value

No return value.

Technical Details

DOM Version Core Level 1 Element Object


元素对象参考手册Element Object

Other Extensions