有没有办法用javascript为给定元素提取XPath查询字符串,比如firebug"复制XPath"功能。
由于
答案 0 :(得分:2)
你的意思是说你需要这样的东西:
var getXPath = function(aNode) {
var xpath = '', prevSibling = aNode, position = 1, nodeType = aNode.nodeType, nodeName = aNode.nodeName;
while (prevSibling = prevSibling.previousSibling) {
if (prevSibling.nodeType == nodeType && prevSibling.nodeName == nodeName) {
position += 1;
}
}
xpath = ((nodeType == 3 /* TEXT_NODE */) ? 'text()' : nodeName) + '[' + position + ']' + (xpath.length ? '/' + xpath : '');
if (aNode.parentNode && aNode.parentNode.nodeName != 'BODY') {
return xpath = (getXPath(aNode.parentNode, xpath) + '/' + xpath).toLowerCase();
}
return xpath;
};