Document: phương thức createExpression()

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 này biên dịch một XPathExpression sau đó có thể được sử dụng để đánh giá (lặp lại).

Bạn phải gọi phương thức này trên cùng một tài liệu mà bạn chạy biểu thức.

Cú pháp

js
createExpression(xpathText, namespaceURLMapper)

Tham số

xpathText

Một chuỗi là biểu thức XPath cần được biên dịch.

namespaceURLMapper

Một hàm ánh xạ tiền tố namespace tới URL namespace (hoặc null nếu không cần).

Giá trị trả về

XPathExpression

Ví dụ

js
const xpathExpr = document.createExpression("//div");
const xpathResult = xpathExpr.evaluate(document); // returns an XPathResult object
const nodeContext = document.querySelector("nav");
// Re-using the XPathExpression "xpathExpr"
const otherResult = xpathExpr.evaluate(nodeContext); // returns an XPathResult object

Đặc tả kỹ thuật

Thông số kỹ thuật
DOM
# dom-xpathevaluatorbase-createexpression

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

Xem thêm