定义和用法
nodeType 属性返回节点的类型。
语法
attrObject.nodeType
nodeType取值
以下是一些重要的nodeType的取值。
|
元素节点
|
节点类型取值(nodeType)
|
|---|---|
|
元素element
|
1
|
|
属性attr
|
2
|
|
文本text
|
3
|
|
注释comments
|
8
|
|
文档document
|
9
|
实例
在所有的例子中,我们将使用 XML 文件 books.xml,以及 JavaScript 函数 loadXMLDoc()。
下面的代码片段显示 category 属性的节点名称、节点值以及节点类型:
xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++){
document.write(x.item(i).attributes[0].nodeName);
document.write(" = ");
document.write(x.item(i).attributes[0].nodeValue);
document.write(" (nodetype: ");
document.write(x.item(i).attributes[0].nodeType + ")");
document.write("<br />");
}
以上代码的输出:
category = children (nodetype: 2)category = cooking (nodetype: 2)category = web (nodetype: 2)category = web (nodetype: 2)
本文介绍了DOM中不同类型的节点及其对应的nodeType值,包括元素、属性、文本和注释等节点类型,并通过一个具体实例展示了如何获取这些节点的信息。
1042

被折叠的 条评论
为什么被折叠?



