在javascript中获取元素的xpath [英] Get element's xpath in javascript

查看:76
本文介绍了在javascript中获取元素的xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 javascript 开发,我需要点击元素的 xpath.我知道为了获得 id 我们可以这样做:

I am developing in javascript and I would need to get the xpath of the element clicked. I know that in order to get the id we can do :

element.onclick = function(event)
{
    var target_id = event.target.id;
}

我该怎么做才能获得 xpath ?

How could I do to get the xpath ?

问候.

推荐答案

有很多方法可以使用 XPath 访问元素.例如,您可以通过节点名称或通过其属性或子节点之一的值来访问它.所以你不能指望 javascript 给你其中之一.

There are many ways howto access an element with XPath. For example you can access it by node name or by the value of one of it's attributes or child nodes. So you can not expect that javascript gives you exactly one of them.

但是由于您拥有 id,因此访问元素的最简单的 xpath 查询是:

But as you have the id, the simplest xpath query to access the element would be:

//*[@id="THE_ID"]

这篇关于在javascript中获取元素的xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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