XPathResult: singleNodeValue プロパティ

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.

singleNodeValueXPathResult インターフェイスの読み取り専用プロパティで、 Node 値を返すか、XPathResult.resultTypeANY_UNORDERED_NODE_TYPE または FIRST_ORDERED_NODE_TYPE である結果のノードが一致しなかった場合は null を返します。

返値は Document.evaluate() が返す XPathResultNode 値です。

例外

TYPE_ERR

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

次の例では singleNodeValue プロパティを使用しています。

HTML

html
<div>XPath example</div>
<div>
  Tag name of the element having the text content 'XPath example':
  <output></output>
</div>

JavaScript

js
const xpath = "//*[text()='XPath example']";
const result = document.evaluate(
  xpath,
  document,
  null,
  XPathResult.FIRST_ORDERED_NODE_TYPE,
  null,
);
document.querySelector("output").textContent = result.singleNodeValue.localName;

結果

仕様書

Specification
DOM
# dom-xpathresult-singlenodevalue

ブラウザーの互換性

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
singleNodeValue

Legend

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

Full support
Full support