DOM通过Xpath查找元素
问题描述:
我是DOM查询的新手,我想知道是否可以通过Xpath以与下面提到的代码类似的方式直接查询DOM元素?
I'm new to DOM querying and I'm wondering if it is possible to query DOM elements directly by Xpath in a similar way to the below-mentioned code?
document.getElementById("searchInput");
谢谢!
答
仅使用XPath的等效语句就是
The equivalent statement using only XPath would be
xPathResult = document.evaluate('//*[@id="searchInput"]', document);
if(xPathResult){
element = xPathResult.iterateNext();
}
在MDN上的浏览器中,查看 XPath简介一些示例和用法.
Have a look at the Intro to XPath in the browser on MDN for some more examples and usages.