XPathResult: singleNodeValue 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

XPathResult 介面的只讀 singleNodeValue 屬性,當結果的 XPathResult.resultTypeANY_UNORDERED_NODE_TYPEFIRST_ORDERED_NODE_TYPE 且未匹配到任何節點時,返回一個 Node 值或 null

返回值是 Document.evaluate() 返回的 XPathResultNode 值。

異常

TYPE_ERR

如果 XPathResult.resultType 不是 ANY_UNORDERED_NODE_TYPEFIRST_ORDERED_NODE_TYPE,則會丟擲一個 TYPE_ERR 型別的 DOMException

示例

下面的示例展示了 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;

結果

規範

規範
DOM
# dom-xpathresult-singlenodevalue

瀏覽器相容性