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 July 2015.

Die snapshotItem()-Methode der XPathResult-Schnittstelle gibt ein Element der Snapshot-Sammlung oder null zurück, falls der Index nicht innerhalb des Bereichs der Knoten liegt. Im Gegensatz zum Iterator-Ergebnis wird der Snapshot nicht ungültig, kann jedoch nach einer Änderung des Dokuments möglicherweise nicht mehr mit diesem übereinstimmen.

Syntax

js
snapshotItem(i)

Parameter

i

Eine Zahl, der Index des Elements.

Rückgabewert

Der Node mit dem angegebenen Index innerhalb der Knotensammlung des XPathResult.

Ausnahmen

TYPE_ERR

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

Beispiele

Das folgende Beispiel zeigt die Verwendung 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

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
snapshotItem

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support