Node是一个接口,许多DOM类型从这个接口继承,并允许类似地处理(或测试)这些各种类型。
以下接口都从Node继承其方法和属性:
Document, Element, CharacterData (which Text, Comment, and CDATASection inherit), ProcessingInstruction, DocumentFragment, DocumentType, Notation, Entity, EntityReference
在方法和属性不相关的特定情况下,这些接口可能返回null。它们可能会抛出异常 - 例如,当将子节点添加到没有子节点的节点类型时。
属性
从其父类EventTarget[1]继承属性。
返回一个表示基础URL的DOMString。 在HTML中,基础URL表示协议,域名和直到最后一个'/'的文件结构。
(网页内容不可用)只读的nsIURI对象表示该元素的基础URI。
Node.firstChildNode.lastChildNode.localNameNode.namespaceURINode.nextSiblingNode.nodeNameNode.nodePrincipalNode.nodeTypeNode.nodeValueNode.ownerDocumentNode.parentNodeNode.parentElementNode.prefixNode.previousSiblingNode.textContent
方法
从其父类EventTarget[1]继承方法。
Node.appendChildNode.cloneNodeNode.compareDocumentPositionNode.containsNode.getFeature已废弃 Gecko 7.0Node.getUserDataNode.hasAttributesNode.hasChildNodesNode.insertBeforeNode.isDefaultNamespaceNode.isEqualNodeNode.isSameNode已废弃 Gecko 9.0 已废弃 Gecko 10.0Node.isSupportedNode.lookupPrefixNode.lookupNamespaceURINode.normalizeNode.removeChildNode.replaceChildNode.setUserData
常量
| 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 |
1 |
DOCUMENT_POSITION_PRECEDING |
2 |
DOCUMENT_POSITION_FOLLOWING |
4 |
DOCUMENT_POSITION_CONTAINS |
8 |
DOCUMENT_POSITION_CONTAINED_BY |
16 |
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC |
32 |
示例
浏览所有子节点
下面这个函数使用递归的方式遍历一个节点的所有子节点,并对他们执行一个回调函数(包括这个父节点本身)。
function DOMComb (oParent, oCallback) {
if (oParent.hasChildNodes()) {
for (var oNode = oParent.firstChild; oNode; oNode = oNode.nextSibling) {
DOMComb(oNode, oCallback);
}
}
oCallback.call(oParent);
}
语法
DOMComb(parentNode, callbackFunction);
描述
使用递归遍历parentNode的所有子节点(包括这个parentNode本身),并对他们(作为 this 对象)执行callbackFunction。
参数
实例
下面的例子会使用console.log打印文档主体的文本内容。
function printContent () {
if (this.nodeValue) { console.log(this.nodeValue); }
}
onload = function () {
DOMComb(document.body, printContent);
};
规范
规范
| Specification | Status | Comment |
|---|---|---|
| DOM Node |
Living Standard | Removed the following properties: attributes, namespaceURI, prefix, and localName.Removed the following methods: isSupported(), hasAttributes(), getFeature(), setUserData(), and getUserData(). |
| Document Object Model (DOM) Level 3 Core Specification Node |
Recommendation | The methods insertBefore(), replaceChild(), removeChild(), and appendChild() returns one more kind of error (NOT_SUPPORTED_ERR) if called on a Document.The normalize() method has been modified so that Text node can also be normalized if the proper DOMConfiguration flag is set.Added the following methods: compareDocumentPosition(), isSameNode(), lookupPrefix(), isDefaultNamespace(), lookupNamespaceURI(), isEqualNode(), getFeature(), setUserData(), and getUserData().Added the following properties: baseURI and textContent. |
| Document Object Model (DOM) Level 2 Core Specification Node |
Recommendation | The ownerDocument property was slightly modified so that DocumentFragment also returns null.Added the following properties: namespaceURI, prefix, and localName.Added the following methods: normalize(), isSupported() and hasAttributes(). |
| Document Object Model (DOM) Level 1 Specification Node |
Recommendation | Initial definition |
浏览器兼容性
未实现22.0 (22.0)
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari | |
|---|---|---|---|---|---|---|---|
| Basic support | (Yes)[1] | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes)[1] | (Yes)[1] | |
getFeature() |
未实现 | ? | 1.0 (1.7 or earlier) 未实现7.0 (7.0) |
? | 未实现 | 未实现 | |
getUserData(), setUserData()and hasAttributes() |
未实现 | ? | 1.0 (1.7 or earlier) 未实现22.0 (22.0) |
? | 未实现 | 未实现 | |
isSameNode() |
(Yes) | ? | 1.0 (1.7 or earlier) Removed in 10 (10) Returned in 48 (48) |
? | 未实现 | 未实现 | |
isSupported() |
未实现 | ? | 1.0 (1.7 or earlier) | ? | ? | ? | ? |
attributes |
未实现 | ? | 1.0 (1.7 or earlier) 未实现22.0 (22.0)[2] |
未实现 | 未实现 | 未实现 | |
rootNode() |
? | ? | CompatGeckoDesktop(48)}} | ? | ? | ? | |
namespaceURI, localName, prefix |
(Yes) 未实现46.0[3] |
? | (Yes) 未实现48.0[3] |
? | ? | ? | |
getRootNode |
54.0 | ? | ? | ? | 41 | ? |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | ? | (Yes)[1] | (Yes) | 1.0 (1.0) | (Yes) | (Yes)[1] | (Yes)[1] | (Yes)[1] |
getFeature() |
未实现 | 未实现 | ? | 1.0 (1.0) 未实现7.0 (7.0) |
? | 未实现 | 未实现 | 未实现 |
getUserData(), setUserData()and hasAttributes() |
未实现 | 未实现 | ? | ? | ? | ? | ? | 未实现 |
isSameNode() |
? | (Yes) | ? | ? | ? | ? | (Yes) | |
isSupported() |
? | 未实现 | ? | ? | ? | ? | ? | 未实现 |
attributes |
? | 未实现 | ? | ? | ? | ? | ? | 未实现 |
rootNode() |
? | 未实现 | ? | 48.0 (48) | ? | ? | ? | 未实现 |
namespaceURI, localName, prefix |
? | 未实现 | ? | (Yes) 未实现48.0[3] |
? | ? | ? | 未实现 |
getRootNode() |
未实现 | 54.0 | ? | ? | ? | 41 | ? | 54.0 |
[1] WebKit and old versions of Blink incorrectly do not make Node inherit from EventTarget.
[2] In Gecko 22.0 (Firefox 22.0 / Thunderbird 22.0 / SeaMonkey 2.19) the attributes property was moved to Element.
[3] The properties were moved to the Element and Attr APIs according to the DOM4 standard.