mozilla

Revision 386917 of Node

  • Revision slug: DOM/Node
  • Revision title: Node
  • Revision id: 386917
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment link to baseURIObject, mark as non-standard

Revision Content

{{domref}}

A Node is an interface from which a number of DOM types inherit, and allows these various types to be treated (or tested) similarly.
The following all inherit this interface and its methods and properties (though they may return null in particular cases where not relevant; or throw an exception when adding children to a node type for which no children can exist): {{domxref("Document")}}, {{domxref("Element")}}, {{domxref("Attr")}}, {{domxref("CharacterData")}} (which {{domxref("Text")}}, {{domxref("Comment")}}, and {{domxref("CDATASection")}} inherit), {{domxref("ProcessingInstruction")}}, {{domxref("DocumentFragment")}}, {{domxref("DocumentType")}}, {{domxref("Notation")}}, {{domxref("Entity")}}, {{domxref("EntityReference")}}

Properties

  • {{domxref("Node.attributes")}}
  • {{domxref("Node.baseURI")}}
  • {{domxref("Node.baseURIObject")}} {{Non-standard_inline()}}
  • {{domxref("Node.childNodes")}}
  • {{domxref("Node.firstChild")}}
  • {{domxref("Node.lastChild")}}
  • {{domxref("Node.localName")}}
  • {{domxref("Node.namespaceURI")}}
  • {{domxref("Node.nextSibling")}}
  • {{domxref("Node.nodeName")}}
  • {{domxref("Node.nodePrincipal")}} {{Non-standard_inline()}}
  • {{domxref("Node.nodeType")}}
  • {{domxref("Node.nodeValue")}}
  • {{domxref("Node.ownerDocument")}}
  • {{domxref("Node.parentNode")}}
  • {{domxref("Node.parentElement")}}
  • {{domxref("Node.prefix")}}
  • {{domxref("Node.previousSibling")}}
  • {{domxref("Node.textContent")}}

Methods

  • {{domxref("Node.appendChild")}}
  • {{domxref("Node.cloneNode")}}
  • {{domxref("Node.compareDocumentPosition")}}
  • {{domxref("Node.contains")}}
  • {{domxref("Node.getFeature")}} {{obsolete_inline("7.0")}}
  • {{domxref("Node.getUserData")}}
  • {{domxref("Node.hasAttributes")}} {{obsolete_inline("22.0")}}
  • {{domxref("Node.hasChildNodes")}}
  • {{domxref("Node.insertBefore")}}
  • {{domxref("Node.isDefaultNamespace")}}
  • {{domxref("Node.isEqualNode")}}
  • {{domxref("Node.isSameNode")}} {{deprecated_inline("9.0")}} {{obsolete_inline("10.0")}}
  • {{domxref("Node.isSupported")}}  {{obsolete_inline("22.0")}}
  • {{domxref("Node.lookupPrefix")}}
  • {{domxref("Node.lookupNamespaceURI")}}
  • {{domxref("Node.normalize")}}
  • {{domxref("Node.removeChild")}}
  • {{domxref("Node.replaceChild")}}
  • {{domxref("Node.setUserData")}}

Constants

See also {{domxref("Node.nodeType")}}

Name Value
ELEMENT_NODE 1
ATTRIBUTE_NODE 2
TEXT_NODE 3
DATA_SECTION_NODE 4
ENTITY_REFERENCE_NODE 5
ENTITY_NODE 6
PROCESSING_INSTRUCTION_NODE 7
COMMENT_NODE 8
DOCUMENT_NODE 9
DOCUMENT_TYPE_NODE 10
DOCUMENT_FRAGMENT_NODE 11
NOTATION_NODE 12
DOCUMENT_POSITION_DISCONNECTED 0x01
DOCUMENT_POSITION_PRECEDING 0x02
DOCUMENT_POSITION_FOLLOWING 0x04
DOCUMENT_POSITION_CONTAINS 0x08
DOCUMENT_POSITION_CONTAINED_BY 0x10
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 0x20

Code snippets

Browse all child nodes

The following function recursively cycle all child nodes of a node and execute a callback function upon them (and upon the parent node itself).

function DOMComb (oParent, oCallback) {
  if (oParent.hasChildNodes()) {
    for (var oNode = oParent.firstChild; oNode; oNode = oNode.nextSibling) {
      DOMComb(oNode, oCallback);
    }
  }
  oCallback.call(oParent);
}

Syntax

DOMComb(parentNode, callbackFunction);

Description

Recursively cycle all child nodes of parentNode and parentNode itself and execute the callbackFunction upon them as this objects.

Parameters

parentNode
The parent node (Node Object).
callbackFunction
The callback function (Function).

Sample usage

The following example send to the console.log the text content of the body:

function printContent () {
  if (this.nodeValue) { console.log(this.nodeValue); }
}

onload = function () {
  DOMComb(document.body, printContent);
};

Specifications

Revision Source

<div>
  {{domref}}</div>
<p>A <code>Node</code> is an interface from which a number of DOM types inherit, and allows these various types to be treated (or tested) similarly.<br />
  The following all inherit this interface and its methods and properties (though they may return null in particular cases where not relevant; or throw an exception when adding children to a node type for which no children can exist): {{domxref("Document")}}, {{domxref("Element")}}, {{domxref("Attr")}}, {{domxref("CharacterData")}} (which {{domxref("Text")}}, {{domxref("Comment")}}, and {{domxref("CDATASection")}} inherit), {{domxref("ProcessingInstruction")}}, {{domxref("DocumentFragment")}}, {{domxref("DocumentType")}}, {{domxref("Notation")}}, {{domxref("Entity")}}, {{domxref("EntityReference")}}</p>
<h2 id="Properties">Properties</h2>
<ul>
  <li>{{domxref("Node.attributes")}}</li>
  <li>{{domxref("Node.baseURI")}}</li>
  <li>{{domxref("Node.baseURIObject")}} {{Non-standard_inline()}}</li>
  <li>{{domxref("Node.childNodes")}}</li>
  <li>{{domxref("Node.firstChild")}}</li>
  <li>{{domxref("Node.lastChild")}}</li>
  <li>{{domxref("Node.localName")}}</li>
  <li>{{domxref("Node.namespaceURI")}}</li>
  <li>{{domxref("Node.nextSibling")}}</li>
  <li>{{domxref("Node.nodeName")}}</li>
  <li>{{domxref("Node.nodePrincipal")}} {{Non-standard_inline()}}</li>
  <li>{{domxref("Node.nodeType")}}</li>
  <li>{{domxref("Node.nodeValue")}}</li>
  <li>{{domxref("Node.ownerDocument")}}</li>
  <li>{{domxref("Node.parentNode")}}</li>
  <li>{{domxref("Node.parentElement")}}</li>
  <li>{{domxref("Node.prefix")}}</li>
  <li>{{domxref("Node.previousSibling")}}</li>
  <li>{{domxref("Node.textContent")}}</li>
</ul>
<h2 id="Methods">Methods</h2>
<ul>
  <li>{{domxref("Node.appendChild")}}</li>
  <li>{{domxref("Node.cloneNode")}}</li>
  <li>{{domxref("Node.compareDocumentPosition")}}</li>
  <li>{{domxref("Node.contains")}}</li>
  <li>{{domxref("Node.getFeature")}} {{obsolete_inline("7.0")}}</li>
  <li>{{domxref("Node.getUserData")}}</li>
  <li>{{domxref("Node.hasAttributes")}} {{obsolete_inline("22.0")}}</li>
  <li>{{domxref("Node.hasChildNodes")}}</li>
  <li>{{domxref("Node.insertBefore")}}</li>
  <li>{{domxref("Node.isDefaultNamespace")}}</li>
  <li>{{domxref("Node.isEqualNode")}}</li>
  <li>{{domxref("Node.isSameNode")}} {{deprecated_inline("9.0")}} {{obsolete_inline("10.0")}}</li>
  <li>{{domxref("Node.isSupported")}}&nbsp; {{obsolete_inline("22.0")}}</li>
  <li>{{domxref("Node.lookupPrefix")}}</li>
  <li>{{domxref("Node.lookupNamespaceURI")}}</li>
  <li>{{domxref("Node.normalize")}}</li>
  <li>{{domxref("Node.removeChild")}}</li>
  <li>{{domxref("Node.replaceChild")}}</li>
  <li>{{domxref("Node.setUserData")}}</li>
</ul>
<h2 id="Constants">Constants</h2>
<p>See also {{domxref("Node.nodeType")}}</p>
<table class="standard-table" summary="A list of constants on Node">
  <tbody>
    <tr>
      <th>Name</th>
      <th>Value</th>
    </tr>
    <tr>
      <td><code>ELEMENT_NODE</code></td>
      <td>1</td>
    </tr>
    <tr>
      <td><code>ATTRIBUTE_NODE</code></td>
      <td>2</td>
    </tr>
    <tr>
      <td><code>TEXT_NODE</code></td>
      <td>3</td>
    </tr>
    <tr>
      <td><code>DATA_SECTION_NODE</code></td>
      <td>4</td>
    </tr>
    <tr>
      <td><code>ENTITY_REFERENCE_NODE</code></td>
      <td>5</td>
    </tr>
    <tr>
      <td><code>ENTITY_NODE</code></td>
      <td>6</td>
    </tr>
    <tr>
      <td><code>PROCESSING_INSTRUCTION_NODE</code></td>
      <td>7</td>
    </tr>
    <tr>
      <td><code>COMMENT_NODE</code></td>
      <td>8</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_NODE</code></td>
      <td>9</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_TYPE_NODE</code></td>
      <td>10</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_FRAGMENT_NODE</code></td>
      <td>11</td>
    </tr>
    <tr>
      <td><code>NOTATION_NODE</code></td>
      <td>12</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_DISCONNECTED</code></td>
      <td>0x01</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_PRECEDING</code></td>
      <td>0x02</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_FOLLOWING</code></td>
      <td>0x04</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_CONTAINS</code></td>
      <td>0x08</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_CONTAINED_BY</code></td>
      <td>0x10</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC</code></td>
      <td>0x20</td>
    </tr>
  </tbody>
</table>
<h2 id="Code_snippets">Code snippets</h2>
<h3 id="Browse_all_child_nodes">Browse all child nodes</h3>
<p>The following function recursively cycle all child nodes of a node and execute a callback function upon them (and upon the parent node itself).</p>
<pre class="brush: js">
function DOMComb (oParent, oCallback) {
&nbsp; if (oParent.hasChildNodes()) {
&nbsp;&nbsp;&nbsp; for (var oNode = oParent.firstChild; oNode; oNode = oNode.nextSibling) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DOMComb(oNode, oCallback);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
&nbsp; oCallback.call(oParent);
}</pre>
<h4 id="Syntax">Syntax</h4>
<pre>
DOMComb(parentNode, callbackFunction);</pre>
<h4 id="Description">Description</h4>
<p>Recursively cycle all child nodes of <code>parentNode</code> and <code>parentNode</code> itself and execute the <code>callbackFunction</code> upon them as <a href="/en-US/docs/JavaScript/Reference/Operators/this" title="en-US/docs/JavaScript/Reference/Operators/this"><code>this</code></a> objects.</p>
<h4 id="Parameters">Parameters</h4>
<dl>
  <dt>
    <code>parentNode</code></dt>
  <dd>
    The parent node (<code><strong>Node</strong> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="en-US/docs/JavaScript/Reference/Global_Objects/Object">Object</a></code>).</dd>
  <dt>
    <code>callbackFunction</code></dt>
  <dd>
    The callback function (<a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="en-US/docs/JavaScript/Reference/Global_Objects/Function"><code>Function</code></a>).</dd>
</dl>
<h4 id="Sample_usage">Sample usage</h4>
<p>The following example send to the <code>console.log</code> the text content of the body:</p>
<pre class="brush: js">
function printContent () {
&nbsp; if (this.nodeValue) { console.log(this.nodeValue); }
}

onload = function () {
&nbsp; DOMComb(document.body, printContent);
};</pre>
<h2 id="Specifications">Specifications</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-1950641247">DOM Level 1 Core: Node interface</a></li>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247">DOM Level 2 Core: Node interface</a></li>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247">DOM Level 3 Core: Node interface</a></li>
  <li><a href="http://dom.spec.whatwg.org/#interface-node" title="http://dom.spec.whatwg.org/#interface-node">DOM Standard: Node Interface</a></li>
</ul>
Revert to this revision