Latest web development tutorials

XML DOM item () method

NodeList Object Reference NodeList objects

Definition and Usage

item () method returns the index number of the specified node list node.

grammar

item(index)

参数 描述
index 索引。


Tips and Notes

Note: Firefox and most other browsers, the empty spaces between the nodes will generate or wrap as text nodes, while Internet Explorer will ignore whitespace text nodes between nodes generated.Thus, in the example below, we'll use a function that checks the node type.

Node type element node is 1, so if the child node is not an element node, it will move to the next node, and checks if this node is an element node. Through this method, we can get the right results in all browsers.

Tip: For more information about browser differences, please visit us in our XML DOM tutorial DOM browser section.


Examples

The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc, and loop through the <bookstore> element in all child element node:

Examples

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement.childNodes;

for (i=0;i<x.length;i++)
{
//Display only element nodes
if (x.item(i).nodeType==1)
{
document.write(x.item(i).nodeName);
document.write("
");
}
}

Output:

book
book
book
book

try it"

NodeList Object Reference NodeList objects