DOM通过Xpath查找元素 [英] DOM find element by Xpath

查看:90
本文介绍了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.

这篇关于DOM通过Xpath查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆