Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM attribute object

HTML DOM attribute object


HTML DOM Nodes

In the HTML DOM (Document Object Model), everything is Node:


Attr object

In the HTML DOM, Attr object Represents an HTML attribute.

HTML attributes always belong to HTML elements.


NamedNodeMap object

In the HTML DOM, the NamedNodeMap object represents an unordered list of nodes.

We can access the nodes in the NamedNodeMap by node name.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the Attr object and the NamedNodeMap object.


Attribute / Method Description
attr.isId If the attribute is of ID type, the isId property returns true, otherwise returns false.
attr.name Returns the attribute name
attr.value Sets or returns the attribute value
attr.specified Returns true if the attribute is specified, otherwise returns false
   
nodemap.getNamedItem() Returns the specified attribute node from the node list.
nodemap.item() Returns the node at the specified index in the node list.
nodemap.length Returns the number of nodes in the node list.
nodemap.removeNamedItem() Removes the specified attribute node
nodemap.setNamedItem() Sets the specified attribute node (by name)


DOM 4 warning !!!

In the W3C DOM core, the Attr (attribute) object inherits all properties and methods of the node object.

In DOM 4, the Attr (attribute) object no longer inherits from the node object.

Considering long-term code quality, you should avoid using node object properties and methods in the attribute object:

Attribute / Method Reason to avoid
attr.appendChild() Attributes have no child nodes
attr.attributes Attributes have no attributes
attr.baseURI Use document.baseURI instead
attr.childNodes Attributes have no child nodes
attr.cloneNode() Use attr.value instead
attr.firstChild Attributes have no child nodes
attr.hasAttributes() Attributes have no attributes
attr.hasChildNodes Attributes have no child nodes
attr.insertBefore() Attributes have no child nodes
attr.isEqualNode() Meaningless
attr.isSameNode() Meaningless
attr.isSupported() Usually true
attr.lastChild Attributes have no child nodes
attr.nextSibling Attributes have no sibling nodes
attr.nodeName Use attrUse .name instead
attr.nodeType Usually 2 (ATTRIBUTE-NODE)
attr.nodeValue Use attrUse .value instead
attr.normalize() Attributes have no specification
attr.ownerDocument Usually your HTML document
attr.ownerElement The HTML element you use to access the attribute
attr.parentNode The HTML element you use to access the attribute
attr.previousSibling Attributes have no sibling nodes
attr.removeChild Attributes have no child nodes
attr.replaceChild Attributes have no child nodes
attr.textContent Use attrUse .value instead