我正在使用 javascript 进行开发,我需要获取单击的元素的 xpath。
我知道为了获得 id 我们可以这样做:
element.onclick = function(event)
{
var target_id = event.target.id;
}
我该如何获取 xpath ?
Regards.
Here:
function getXPath(node){
if(node.hasAttribute("id")){
return '//' + node.tagName + '[@id="' + node.id + '"]';
}
if(node.hasAttribute("class")){
return '//' + node.tagName + '[@class="' + node.getAttribute("class") + '"]';
}
var old = '/' + node.tagName;
var new_path = this.xpath(node.parentNode) + old;
return new_path;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)