Node Object

Definition and Usage

The Node object represents a node in the HTML document.

A node in an HTML document is:

  • The Document
  • An element
  • An attribute
  • Text
  • A comment

All of the node types are explained in the Node Types reference at the bottom of this page.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The Node object is supported in all major browsers.


Node Object Properties

The "DOM" column indicates in which DOM Level the property was introduced.

Property Description DOM
attributes Returns a collection of a node's attributes 1
baseURI Returns the absolute base URI of a node 3
childNodes Returns a NodeList of child nodes for a node 1
firstChild Returns the first child of a node 1
lastChild Returns the last child of a node 1
localName Returns the local part of the name of a node 2
namespaceURI Returns the namespace URI of a node 2
nextSibling Returns the next node at the same node tree level 1
nodeName Returns the name of a node, depending on its type 1
nodeType Returns the type of a node 1
nodeValue Sets or returns the value of a node, depending on its type 1
ownerDocument Returns the root element (document object) for a node 2
parentNode Returns the parent node of a node 1
prefix Sets or returns the namespace prefix of a node 2
previousSibling Returns the previous node at the same node tree level 1
textContent Sets or returns the textual content of a node and its descendants 3

Node Object Methods

The "DOM" column indicates in which DOM Level the method was introduced.

Method Description DOM
appendChild() Adds a new child node, to the specified node, as the last child node 1
cloneNode() Clones a node 1
compareDocumentPosition() Compares the document position of two nodes 1
getFeature(feature,version) Returns a DOM object which implements the specialized APIs of the specified feature and version 3
getUserData(key) Returns the object associated to a key on a this node. The object must first have been set to this node by calling setUserData with the same key 3
hasAttributes() Returns true if a node has any attributes, otherwise it returns false 2
hasChildNodes() Returns true if a node has any child nodes, otherwise it returns false 1
insertBefore() Inserts a new child node before a specified, existing, child node 1
isDefaultNamespace() Returns true if the specified namespaceURI is the default, otherwise false 3
isEqualNode() Checks if two nodes are equal 3
isSameNode() Checks if two nodes are the same node 3
isSupported() Returns true if a specified feature is supported on a node, otherwise false 2
lookupNamespaceURI() Returns the namespace URI matching a specified prefix 3
lookupPrefix() Returns the prefix matching a specified namespace URI 3
normalize() Joins adjacent text nodes and removes empty text nodes 2
removeChild() Removes a child node 1
replaceChild() Replaces a child node 1
setUserData(key,data,handler) Associates an object to a key on a node 3


Node Types

Documents, elements, attributes, and other aspects of an HTML document has different node types.

There are 12 different node types, which may have children of various node types:

Node type Description Children
1 Element Represents an element Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference
2 Attr Represents an attribute Text, EntityReference
3 Text Represents textual content in an element or attribute None
4 CDATASection Represents a CDATA section in a document (text that will NOT be parsed by a parser) None
5 EntityReference Represents an entity reference Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
6 Entity Represents an entity Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
7 ProcessingInstruction Represents a processing instruction None
8 Comment Represents a comment None
9 Document Represents the entire document (the root-node of the DOM tree) Element, ProcessingInstruction, Comment, DocumentType
10 DocumentType Provides an interface to the entities defined for the document None
11 DocumentFragment Represents a "lightweight" Document object, which can hold a portion of a document Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
12 Notation Represents a notation declared in the DTD None

Node Types - Return Values

The return value of the nodeName and the nodeValue properties for each node type:

Node type nodeName returns nodeValue returns
1 Element element name null
2 Attr attribute name attribute value
3 Text #text content of node
4 CDATASection #cdata-section content of node
5 EntityReference entity reference name null
6 Entity entity name null
7 ProcessingInstruction target content of node
8 Comment #comment comment text
9 Document #document null
10 DocumentType doctype name null
11  DocumentFragment #document fragment null
12 Notation notation name null

NodeTypes - Named Constants

NodeType Named Constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值