Node
Node
是一個被多種 DOM 類型繼承的介面,它讓各種類型的 DOM 都能以同樣的方式來操作。如繼承了相同的方法,或能以相同的方式測試。
Node
繼承自 EventTarget
,而繼承了 Node
的屬性及方法的介面則有:Document
、Element
、CharacterData
(被 Text
(en-US)、Comment
(en-US) 以及 CDATASection
(en-US) 所繼承)、ProcessingInstruction
(en-US)、DocumentFragment
、DocumentType
、Notation
、Entity
、EntityReference
。
These interfaces may return null in particular cases where the methods and properties are not relevant. They may throw an exception - for example when adding children to a node type for which no children can exist.
屬性
Inherits properties from its parents EventTarget
.[1]
Node.baseURI
(en-US) Read only- : Returns a
DOMString
representing the base URL. The concept of base URL changes from one language to another; in HTML, it corresponds to the protocol, the domain name and the directory structure, that is all until the last'/'
.
- : Returns a
Node.baseURIObject
(en-US) 非標準- : (Not available to web content.) The read-only
nsIURI
object representing the base URI for the element.
- : (Not available to web content.) The read-only
Node.childNodes
Read onlyNode.firstChild
(en-US) Read only- : Returns a
Node
representing the first direct child node of the node, ornull
if the node has no child.
- : Returns a
Node.lastChild
(en-US) Read only- : Returns a
Node
representing the last direct child node of the node, ornull
if the node has no child.
- : Returns a
Node.nextSibling
(en-US) Read only- : Returns a
Node
representing the next node in the tree, ornull
if there isn't such node.
- : Returns a
Node.nodeName
Read only- : Returns a
DOMString
containing the name of theNode
. The structure of the name will differ with the node type. E.g. AnHTMLElement
will contain the name of the corresponding tag, like'audio'
for anHTMLAudioElement
(en-US), aText
(en-US) node will have the'#text'
string, or aDocument
node will have the'#document'
string.
- : Returns a
Node.nodePrincipal
(en-US) 非標準- : A
nsIPrincipal
representing the node principal.
- : A
Node.nodeType
Read only-
: Returns an unsigned short
representing the type of the node. Possible values are:Name Value ELEMENT_NODE
1
ATTRIBUTE_NODE
已棄用2
TEXT_NODE
3
CDATA_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
-
Node.nodeValue
(en-US)- : Returns / Sets the value of the current node
Node.ownerDocument
Read only- : Returns the
Document
that this node belongs to. If no document is associated with it, returnsnull
.
- : Returns the
Node.parentNode
(en-US) Read only- : Returns a
Node
that is the parent of this node. If there is no such node, like if this node is the top of the tree or if doesn't participate in a tree, this property returnsnull
.
- : Returns a
Node.parentElement
(en-US) Read onlyNode.previousSibling
(en-US) Read only- : Returns a
Node
representing the previous node in the tree, ornull
if there isn't such node.
- : Returns a
Node.textContent
- : Returns / Sets the textual content of an element and all its descendants.
Deprecated properties
Node.rootNode
(en-US) Read only 已棄用-
Returns a
Node
object representing the topmost node in the tree, or the current node if it's the topmost node in the tree. This has been replaced byNode.getRootNode()
(en-US).
Obsolete properties
Node.localName
(en-US) 已棄用 Read only-
Returns a
DOMString
representing the local part of the qualified name of an element.備註: In Firefox 3.5 and earlier, the property upper-cases the local name for HTML elements (but not XHTML elements). In later versions, this does not happen, so the property is in lower case for both HTML and XHTML.
Node.namespaceURI
(en-US) 已棄用 Read only-
The namespace URI of this node, or
null
if it is no namespace.備註: In Firefox 3.5 and earlier, HTML elements are in no namespace. In later versions, HTML elements are in the
https://www.w3.org/1999/xhtml/
namespace in both HTML and XML trees. Node.prefix
(en-US) 已棄用 Read only-
Is a
DOMString
representing the namespace prefix of the node, ornull
if no prefix is specified.
方法
Inherits methods from its parent, EventTarget
.[1]
Node.appendChild()
(en-US)-
Adds the specified childNode argument as the last child to the current node. If the argument referenced an existing node on the DOM tree, the node will be detached from its current position and attached at the new position.
Node.cloneNode()
-
Clone a
Node
, and optionally, all of its contents. By default, it clones the content of the node. Node.compareDocumentPosition()
(en-US)-
Compares the position of the current node against another node in any other document.
Node.contains()
(en-US)-
Returns a
Boolean
value indicating whether a node is a descendant of a given node or not. Node.getRootNode()
(en-US)-
Returns the context object's root which optionally includes the shadow root if it is available.
Node.hasChildNodes()
(en-US)-
Returns a
Boolean
indicating if the element has any child nodes, or not. Node.insertBefore()
-
Inserts a
Node
before the reference node as a child of the current node. Node.isDefaultNamespace()
(en-US)-
Accepts a namespace URI as an argument and returns a
Boolean
with a value oftrue
if the namespace is the default namespace on the given node orfalse
if not. Node.isEqualNode()
(en-US)-
Returns a
Boolean
which indicates whether or not two nodes are of the same type and all their defining data points match. Node.isSameNode()
(en-US)-
Returns a
Boolean
value indicating whether or not the two nodes are the same (that is, they reference the same object). Node.lookupPrefix()
(en-US)-
Returns a
DOMString
containing the prefix for a given namespace URI, if present, andnull
if not. When multiple prefixes are possible, the result is implementation-dependent. Node.lookupNamespaceURI()
(en-US)-
Accepts a prefix and returns the namespace URI associated with it on the given node if found (and
null
if not). Supplyingnull
for the prefix will return the default namespace. Node.normalize()
(en-US)-
Clean up all the text nodes under this element (merge adjacent, remove empty).
Node.removeChild()
-
Removes a child node from the current element, which must be a child of the current node.
Node.replaceChild()
(en-US)-
Replaces one child
Node
of the current one with the second one given in parameter.
Obsolete methods
Node.getFeature()
已棄用-
x
Node.getUserData()
已棄用-
Allows a user to get some
DOMUserData
from the node. Node.hasAttributes()
已棄用-
Returns a
Boolean
indicating if the element has any attributes, or not. Node.isSupported()
(en-US) 已棄用-
Returns a
Boolean
flag containing the result of a test whether the DOM implementation implements a specific feature and this feature is supported by the specific node. Node.setUserData()
已棄用-
Allows a user to attach, or remove,
DOMUserData
to the node.
範例
Browse all child nodes
The following function recursively cycles all child nodes of a node and executes 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
(en-US) 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);
};
Remove all children nested within a node
Element.prototype.removeAll = function () {
while (this.firstChild) { this.removeChild(this.firstChild); }
return this;
};
Sample usage
/* ... an alternative to document.body.innerHTML = "" ... */
document.body.removeAll();
規範
Specification |
---|
DOM Standard # interface-node |
瀏覽器相容性
BCD tables only load in the browser