XPathResult: iterateNext() メソッド

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.

iterateNext()XPathResult インターフェイスのメソッドで、ノード集合の結果を反復処理し、そこから次のノードを返すか、まだノードがない場合は null を返します。

構文

js
iterateNext()

引数

なし。

返値

XPathResult ノード集合の中の次の Node です。

例外

TYPE_ERR

XPathResult.resultTypeUNORDERED_NODE_ITERATOR_TYPE または ORDERED_NODE_ITERATOR_TYPE でない場合、 TYPE_ERR 型の XPathException が発生します。

INVALID_STATE_ERR

結果を返してから文書が変更された場合、 INVALID_STATE_ERR 型の XPathException が発生します。

次の例では iterateNext() メソッドを使用しています。

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

結果

仕様書

Specification
DOM
# dom-xpathresult-iteratenext

ブラウザーの互換性

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
iterateNext

Legend

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

Full support
Full support