Dom-nodelist-item
提供:Dev Guides
DOM-NodeListオブジェクトメソッド-アイテム
メソッド_item_は、コレクション内のindexthアイテムを返します。
構文
以下は、_item_属性を使用するための構文です。
Node item(long index)
_index_はコレクションへのインデックスです。
例
_node.xml_の内容は以下のとおりです-
<?xml version = "1.0"?>
<Company>
<Employee category = "Technical">
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
</Employee>
<Employee category = "Non-Technical">
<FirstName>Taniya</FirstName>
<LastName>Mishra</LastName>
<ContactNo>1234667898</ContactNo>
<Email>[email protected]</Email>
</Employee>
<Employee category = "Management">
<FirstName>Tanisha</FirstName>
<LastName>Sharma</LastName>
<ContactNo>1234562350</ContactNo>
<Email>[email protected]</Email>
</Employee>
</Company>
次の例(nodelist_methods)は、XMLドキュメント(link:/dom/node.xml [node.xml])をXML DOMオブジェクトに解析し、ノードリストの各項目を表示します-
<!DOCTYPE html>
<body>
<script>
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/dom/node.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
y = xmlDoc.getElementsByTagName("Employee");
for (i=0; i<y.length; i++) {
document.write(y.item(i).nodeName);
document.write("<br>");
}
</script>
</body>
</html>
実行
このファイルを_nodemethod_item_としてサーバーパスに保存します(このファイルとlink:/dom/node.xml [node.xml]はサーバーの同じパスにある必要があります)。 次のように出力を取得します-
Employee
Employee
Employee