Dom 简明教程

DOM - Node Object

Node 接口是整个 Document Object Model 的主要数据类型。Node 用于表示整个文档树中的单个 XML 元素。

Node interface is the primary datatype for the entire Document Object Model. The node is used to represent a single XML element in the entire document tree.

一个节点可以是任何类型,即属性节点、文本节点或任何其他节点。属性 nodeName、nodeValue 和 attributes 都包含在机制中,用于获取节点信息,而无需转换为指定派生接口。

A node can be any type that is an attribute node, a text node or any other node. The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface.

Attributes

下表列出了 Node 对象的属性 −

The following table lists the attributes of the Node object −

Attribute

Type

Description

attributes

NamedNodeMap

This is of type NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise. This has been removed. Refer specs

baseURI

DOMString

It is used to specify absolute base URI of the node.

childNodes

NodeList

It is a NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.

firstChild

Node

It specifies the first child of a node.

lastChild

Node

It specifies the last child of a node.

localName

DOMString

It is used to specify the name of the local part of a node. This has been removed. Refer specs.

namespaceURI

DOMString

It specifies the namespace URI of a node. This has been removed. Refer specs

nextSibling

Node

It returns the node immediately following this node. If there is no such node, this returns null.

nodeName

DOMString

The name of this node, depending on its type.

nodeType

unsigned short

It is a code representing the type of the underlying object.

nodeValue

DOMString

It is used to specify the value of a node depending on their types.

ownerDocument

Document

It specifies the Document object associated with the node.

parentNode

Node

This property specifies the parent node of a node.

prefix

DOMString

This property returns the namespace prefix of a node. This has been removed. Refer specs

previousSibling

Node

This specifies the node immediately preceding the current node.

textContent

DOMString

This specifies the textual content of a node.

Node Types

我们已将节点类型列出如下 −

We have listed the node types as below −

  1. ELEMENT_NODE

  2. ATTRIBUTE_NODE

  3. ENTITY_NODE

  4. ENTITY_REFERENCE_NODE

  5. DOCUMENT_FRAGMENT_NODE

  6. TEXT_NODE

  7. CDATA_SECTION_NODE

  8. COMMENT_NODE

  9. PROCESSING_INSTRUCTION_NODE

  10. DOCUMENT_NODE

  11. DOCUMENT_TYPE_NODE

  12. NOTATION_NODE

Methods

以下表格列出了不同的节点对象方法 −

Below table lists the different Node Object methods −

S.No.

Method & Description

1

appendChild(Node newChild)This method adds a node after the last child node of the specified element node. It returns the added node.

2

cloneNode(boolean deep)This method is used to create a duplicate node, when overridden in a derived class. It returns the duplicated node.

3

compareDocumentPosition(Node other)This method is used to compare the position of the current node against a specified node according to the document order. Returns unsigned short, how the node is positioned relatively to the reference node.

4

getFeature(DOMString feature, DOMString version) Returns the DOM Object which implements the specialized APIs of the specified feature and version, if any, or null if there is no object. This has been removed. Refer specs.

5

getUserData(DOMString key) Retrieves the object associated to a key on this node. The object must first have been set to this node by calling the setUserData with the same key. Returns the DOMUserData associated to the given key on this node, or null if there was none. This has been removed. Refer specs.

6

hasAttributes() Returns whether this node (if it is an element) has any attributes or not. Returns true if any attribute is present in the specified node else returns false. This has been removed. Refer specs.

7

hasChildNodes()Returns whether this node has any children. This method returns true if the current node has child nodes otherwise false.

8

insertBefore(Node newChild, Node refChild)This method is used to insert a new node as a child of this node, directly before an existing child of this node. It returns the node being inserted.

9

isDefaultNamespace(DOMString namespaceURI)This method accepts a namespace URI as an argument and returns a Boolean with a value of true if the namespace is the default namespace on the given node or false if not.

10

isEqualNode(Node arg)This method tests whether two nodes are equal. Returns true if the nodes are equal, false otherwise.

11

isSameNode(Node other) This method returns whether current node is the same node as the given one. Returns true if the nodes are the same, false otherwise. This has been removed. Refer specs.

12

isSupported(DOMString feature, DOMString version) This method returns whether the specified DOM module is supported by the current node. Returns true if the specified feature is supported on this node, false otherwise. This has been removed. Refer specs.

13

lookupNamespaceURI(DOMString prefix)This method gets the URI of the namespace associated with the namespace prefix.

14

lookupPrefix(DOMString namespaceURI)This method returns the closest prefix defined in the current namespace for the namespace URI. Returns an associated namespace prefix if found or null if none is found.

15

normalize()Normalization adds all the text nodes including attribute nodes which define a normal form where structure of the nodes which contain elements, comments, processing instructions, CDATA sections, and entity references separates the text nodes, i.e, neither adjacent Text nodes nor empty Text nodes.

16

removeChild(Node oldChild)This method is used to remove a specified child node from the current node. This returns the node removed.

17

replaceChild(Node newChild, Node oldChild)This method is used to replace the old child node with a new node. This returns the node replaced.

18

setUserData(DOMString key, DOMUserData data, UserDataHandler handler) This method associates an object to a key on this node. The object can later be retrieved from this node by calling getUserData with the same key. This returns the DOMUserData previously associated to the given key on this node. This has been removed. Refer specs.