XPathResult: iterateNext() Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Die iterateNext()-Methode der XPathResult-Schnittstelle durchläuft ein Node-Set-Ergebnis und gibt den nächsten Knoten daraus zurück oder null, wenn keine weiteren Knoten vorhanden sind.

Syntax

js
iterateNext()

Parameter

Keine.

Rückgabewert

Der nächste Node innerhalb des Node-Sets des XPathResult.

Ausnahmen

TYPE_ERR

Falls XPathResult.resultType nicht UNORDERED_NODE_ITERATOR_TYPE oder ORDERED_NODE_ITERATOR_TYPE ist, wird eine DOMException vom Typ TYPE_ERR ausgelöst.

INVALID_STATE_ERR

Falls das Dokument verändert wurde, seit das Ergebnis zurückgegeben wurde, wird eine DOMException vom Typ INVALID_STATE_ERR ausgelöst.

Beispiele

Das folgende Beispiel zeigt die Nutzung der iterateNext()-Methode.

HTML

html
<div>XPath example</div>
<div>Tag names of the matched nodes: <output></output></div>

JavaScript

js
const xpath = "//div";
const result = document.evaluate(
  xpath,
  document,
  null,
  XPathResult.ANY_TYPE,
  null,
);
let node = null;
const tagNames = [];
while ((node = result.iterateNext())) {
  tagNames.push(node.localName);
}
document.querySelector("output").textContent = tagNames.join(", ");

Ergebnis

Spezifikationen

Specification
DOM
# dom-xpathresult-iteratenext

Browser-Kompatibilität