Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

XPathResult: snapshotItem() Methode

Baseline Widely available

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

Die snapshotItem() Methode der XPathResult Schnittstelle gibt ein Element der Snapshot-Sammlung zurück oder null, falls der Index nicht innerhalb des Bereichs der Knoten liegt. Im Gegensatz zum Iteratorergebnis wird der Snapshot nicht ungültig, kann jedoch möglicherweise nicht mehr dem aktuellen Dokument entsprechen, wenn dieses verändert wird.

Syntax

js
snapshotItem(i)

Parameter

i

Eine Zahl, der Index des Elements.

Rückgabewert

Der Node am angegebenen Index innerhalb der Knotengruppe des XPathResult.

Ausnahmen

TYPE_ERR

Falls XPathResult.resultType nicht UNORDERED_NODE_SNAPSHOT_TYPE oder ORDERED_NODE_SNAPSHOT_TYPE ist, wird ein DOMException vom Typ TYPE_ERR ausgelöst.

Beispiele

Das folgende Beispiel zeigt die Nutzung der snapshotItem() 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.ORDERED_NODE_SNAPSHOT_TYPE,
  null,
);
let node = null;
const tagNames = [];
for (let i = 0; i < result.snapshotLength; i++) {
  node = result.snapshotItem(i);
  tagNames.push(node.localName);
}
document.querySelector("output").textContent = tagNames.join(", ");

Ergebnis

Spezifikationen

Specification
DOM
# dom-xpathresult-snapshotitem-index-index

Browser-Kompatibilität