javascript学习之nodeType定义和用法

来源:互联网 发布:美国能淘宝吗 编辑:程序博客网 时间:2024/06/03 16:11

定义和用法

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)
原创粉丝点击