ການລຶບປະເພດຂອງ HTML DOM Element removeAttribute()

Definition and usage

removeAttribute() Method removes an attribute from the element.

Tip:The operation of deleting a non-existent attribute or an attribute that is not set but has a default value will be ignored.

The difference between removeAttribute() and removeAttributeNode()

removeAttribute() Method deletes attribute and does not return a value.

removeAttributeNode() Method deletes Attr object and returns the removed object.

The result will be the same.

See also:

Reference manual:

getAttribute() method

setAttribute() method

hasAttribute() method

hasAttributes() method

getAttributeNode() method

setAttributeNode() method

removeAttributeNode() method

Tutorial:

HTML 属性

Example

Example 1

Remove the class attribute from the <h1> element:

document.getElementsByTagName("H1")[0].removeAttribute("class");

Try it yourself

Example 2

Remove the href attribute from the <a> element:

document.getElementById("myAnchor").removeAttribute("href");

Try it yourself

Syntax

element.removeAttribute(name)

Parameter

Parameter Description
name Required. The name of the attribute.

Return value

None.

Throw

If the element is read-only and does not allow deleting its attributes, this method will throw a DOMException exception with code NO_MODIFICATION_ALLOWED_ERR.

Browser support

element.removeAttribute() Is DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support