XPathResult: singleNodeValue property

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.

Thuộc tính chỉ đọc singleNodeValue của giao diện XPathResult trả về một giá trị Node hoặc null trong trường hợp không có nút nào khớp với kết quả có XPathResult.resultTypeANY_UNORDERED_NODE_TYPE hoặc FIRST_ORDERED_NODE_TYPE.

Giá trị

Giá trị trả về là giá trị Node của XPathResult được trả về bởi Document.evaluate().

Ngoại lệ

TYPE_ERR

Trong trường hợp XPathResult.resultType không phải là ANY_UNORDERED_NODE_TYPE hoặc FIRST_ORDERED_NODE_TYPE, một DOMException kiểu TYPE_ERR sẽ được ném ra.

Ví dụ

Ví dụ sau đây cho thấy cách sử dụng thuộc tính 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;

Kết quả

Thông số kỹ thuật

Thông số kỹ thuật
DOM
# dom-xpathresult-singlenodevalue

Tương thích trình duyệt