XPathResult: snapshotItem() method
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.
Phương thức snapshotItem() của giao diện
XPathResult trả về một phần tử của tập hợp ảnh chụp hoặc
null trong trường hợp chỉ số nằm ngoài phạm vi của các nút. Không giống như
kết quả iterator, ảnh chụp không trở nên không hợp lệ, nhưng có thể không tương ứng với
tài liệu hiện tại nếu nó bị thay đổi.
Cú pháp
snapshotItem(i)
Tham số
i-
Một số, chỉ số của phần tử.
Giá trị trả về
Node tại chỉ số đã cho trong tập hợp nút của
XPathResult.
Ngoại lệ
TYPE_ERR
Trong trường hợp XPathResult.resultType không phải là
UNORDERED_NODE_SNAPSHOT_TYPE hoặc ORDERED_NODE_SNAPSHOT_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 phương thức snapshotItem().
HTML
<div>XPath example</div>
<div>Tag names of the matched nodes: <output></output></div>
JavaScript
const xpath = "//div";
const result = document.evaluate(
xpath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null,
);
let node = null;
const tagNames = [];
for (let i = 0; i < result.snapshotLength; i++) {
node = result.snapshotItem(i);
tagNames.push(node.localName);
}
document.querySelector("output").textContent = tagNames.join(", ");
Kết quả
Thông số kỹ thuật
| Specification |
|---|
| DOM> # dom-xpathresult-snapshotitem-index-index> |