XPathResult: snapshotItem() メソッド

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.

snapshotItem()XPathResult インターフェイスのメソッドで、アイテムのスナップショットの集合、またはインデックスがノードの範囲を外れている場合は null を返します。イテレーターの返値とは異なり、スナップショットは無効になることはありませんが、変更したときに現在の文書に対応しない場合があります。

構文

js
snapshotItem(i)

引数

i

数値で、項目のインデックスです。

返値

XPathResult のノード集合内の指定されたインデックスの Node です。

例外

TYPE_ERR

XPathResult.resultTypeUNORDERED_NODE_SNAPSHOT_TYPE または ORDERED_NODE_SNAPSHOT_TYPE でない場合、 XPathException による例外が TYPE_ERR 型で発生します。

以下の例は snapshotItem() メソッドの使用例を表しています。

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(", ");

結果

仕様書

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

ブラウザーの互換性

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