본문 바로가기

JAVASCRIPT

DOM Attribute Object

HTML DOM Nodes

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

  • The document itself is a document node
  • All HTML elements are element nodes
  • All HTML attributes are attribute nodes
  • Text inside HTML elements are text nodes
  • Comments are comment nodes

The Attr Object

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

An HTML attribute always belongs to an HTML element.


The NamedNodeMap Object

In the HTML DOM, the NamedNodeMap object represents an unordered collection of an elements attribute nodes.

Nodes in a NamedNodeMap can be accessed by name or by index (number).


Browser Support

Object
AttrYesYesYesYesYes
NamedNodeMapYesYesYesYesYes

The Attr Object and the NamedNodeMap Object is supported in all major browsers.


Properties and Methods

Property / MethodDescription
attr.isIdReturns true if the attribute is of type Id, otherwise it returns false
attr.nameReturns the name of an attribute
attr.valueSets or returns the value of the attribute
attr.specifiedReturns true if the attribute has been specified, otherwise it returns false
  
nodemap.getNamedItem()Returns a specified attribute node from a NamedNodeMap
nodemap.item()Returns the attribute node at a specified index in a NamedNodeMap
nodemap.lengthReturns the number of attribute nodes in a NamedNodeMap
nodemap.removeNamedItem()Removes a 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 from the Node object.

In DOM 4, the Attr object no longer inherits from Node.

For future code quality, you should avoid using node object properties and methods on attribute objects:

 Property / MethodReason for avoiding
attr.appendChild()Attributes don't have child nodes
attr.attributesAttributes don't have attributes
attr.baseURIuse document.baseURI instead
attr.childNodesAttributes don't have child nodes
attr.cloneNode()Get or set the attr.value instead
attr.firstChildAttributes don't have child nodes
attr.hasAttributes()Attributes don't have attributes
attr.hasChildNodesAttributes don't have child nodes
attr.insertBefore()Attributes don't have child nodes
attr.isEqualNode()Makes no sense
attr.isSameNode()Makes no sense
attr.isSupported()Is always true
attr.lastChildAttributes don't have child nodes
attr.nextSiblingAttributes don't have siblings
attr.nodeNameUse attr.name instead
attr.nodeTypeThis is always 2 (ATTRIBUTE_NODE)
attr.nodeValueUse attr.value instead
attr.normalize()Attributes cannot be normalized
attr.ownerDocumentThis is always your HTML document
attr.ownerElementThis is the HTML element you used to access the attribute
attr.parentNodeThis is the HTML element you used to access the attribute
attr.previousSiblingAttributes don't have siblings
attr.removeChildAttributes don't have child nodes
attr.replaceChildAttributes don't have child nodes
attr.textContentUse attr.value instead


'JAVASCRIPT' 카테고리의 다른 글

DOM Document Object  (0) 2014.12.18
DOM Element Object  (0) 2014.12.18
DOM Event Object Reference  (0) 2014.12.18
DOM  (0) 2014.12.18
디지탈 시계  (0) 2014.09.22