XPathResult: singleNodeValue 屬性
XPathResult 介面的只讀 singleNodeValue 屬性,當結果的 XPathResult.resultType 是 ANY_UNORDERED_NODE_TYPE 或 FIRST_ORDERED_NODE_TYPE 且未匹配到任何節點時,返回一個 Node 值或 null。
值
返回值是 Document.evaluate() 返回的 XPathResult 的 Node 值。
異常
TYPE_ERR
如果 XPathResult.resultType 不是 ANY_UNORDERED_NODE_TYPE 或 FIRST_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 |
瀏覽器相容性
載入中…